CodeIgniter Forums
Divide different type of requests - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forum-29.html)
+--- Thread: Divide different type of requests (/thread-68031.html)



Divide different type of requests - mt19 - 05-13-2017

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)


RE: Divide different type of requests - ciadmin - 05-13-2017

Sounds a lot like https://bcit-ci.github.io/CodeIgniter4/general/routing.html#using-http-verbs-in-routes


RE: Divide different type of requests - mt19 - 05-14-2017

(05-13-2017, 09:18 AM)ciadmin Wrote: Sounds a lot like https://bcit-ci.github.io/CodeIgniter4/general/routing.html#using-http-verbs-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!


RE: Divide different type of requests - sv3tli0 - 05-15-2017

(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/general/routing.html#grouping-routes

Resource routes
https://bcit-ci.github.io/CodeIgniter4/general/routing.html#resource-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...