• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Divide different type of requests

#1
Hello,

this idea comes from ExpressJS (on top of NodeJS).

To define a route in ExpressJS you can do something like the following:

Code:
router.get('/welcome', function(..){..})
router.post('/welcome', function(..){..})
router.put('/welcome', function(..){..})
router.delete('/welcome', function(..){..})
...


I really love this method division, since it helps keeping everything even more organised.
Actually I have mocked this function creating MY_Router.php in application/core with the following:

PHP Code:
class MY_Router extends CI_Router {

 
       public function __construct()
 
       {
 
               parent::__construct();
 
               // Default route method: METHOD_index
 
               $this->set_method('index');
 
       }

 
       public function set_method($method)
    {
 
               $request_method $_SERVER['REQUEST_METHOD'] . '_';
        
$this->method $request_method $method;
    }


Now if I perform a GET request to /welcome/, the Class welcome will be loaded and the Method GET_index will be called.


I think that it would be cool to have this feature in Codeigniter 4 (maybe with a settings that permits to enable and disable it)
Reply

#2
Sounds a lot like https://bcit-ci.github.io/CodeIgniter4/g...-in-routes
Reply

#3
(05-13-2017, 09:18 AM)ciadmin Wrote: Sounds a lot like https://bcit-ci.github.io/CodeIgniter4/g...-in-routes

Yeah, sort of. The difference is that with routing, a developer should add each page.

If a site has 2 pages, like 'main' and 'contact', he should do something like:

PHP Code:
$routes->get('main/(:any)''GET_main/$1');
$routes->post('main/(:any)''POST_main/$1');
$routes->put('main/(:any)''PUT_main/$1');
... 

In my application I have 13 controllers, then my router would be something like 50 lines long.

It is possible that I am loosing some special functions through.

PS, I love the new routing in CI4, great work!
Reply

#4
(05-14-2017, 04:48 AM)mt19 Wrote: In my application I have 13 controllers, then my router would be something like 50 lines long.

It is possible that I am loosing some special functions through.

PS, I love the new routing in CI4, great work!


It doesn't sounds logical a framework to follow some personal needs (in that case yours). 
You can make grouping to your routes and with that to make it more clear or to use resources routes or to extend the router by yourself.

Grouping
https://bcit-ci.github.io/CodeIgniter4/g...ing-routes

Resource routes
https://bcit-ci.github.io/CodeIgniter4/g...rce-routes

Those are helpful features ..

Further if you have some specific logic of your routes in your mind you can follow the "resource" idea and to extend the Router with adding another method as resource() where you can register the group of routes which you have multiple times at your controllers...
Best VPS Hosting : Digital Ocean
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.