CodeIgniter Forums
Do I need to set all my routes in Routes.php? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Do I need to set all my routes in Routes.php? (/showthread.php?tid=74169)

Pages: 1 2


Do I need to set all my routes in Routes.php? - tgix - 08-10-2019

Struggling along trying to recreate my REST API from CI3 in CI4-beta4. I find it very cumbersome to configure all my route entries in Routes.php like this (far from complete):
PHP Code:
$routes->group('users', function ($routes) {
    
$routes->get('list''Users::list');
    
$routes->get('(:num)''Users::get_user/$i');
    
$routes->options('(:any)''Users::options_respond');
}); 
Is there a way to have the routes live in the Controller instead or am I overthinking this?


RE: Do I need to set all my routes in Routes.php? - includebeer - 08-10-2019

If you don’t want to configure all your routes, you can still use “auto-route” like in CI3: https://codeigniter4.github.io/userguide/incoming/routing.html#use-defined-routes-only

PHP Code:
$routes->setAutoRoute(true); 



RE: Do I need to set all my routes in Routes.php? - tgix - 08-10-2019

(08-10-2019, 06:46 AM)includebeer Wrote: If you don’t want to configure all your routes, you can still use “auto-route” like in CI3: https://codeigniter4.github.io/userguide/incoming/routing.html#use-defined-routes-only

PHP Code:
$routes->setAutoRoute(true); 

OK. In CI3 I used chriskacerguis/codeigniter-restserver to handle GET, POST etc.
I guess auto routing will work with GET and I can setup only POST, OPTIONS etc for the special cases?


RE: Do I need to set all my routes in Routes.php? - ciadmin - 08-10-2019

See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#resource-routes


RE: Do I need to set all my routes in Routes.php? - tgix - 08-10-2019

(08-10-2019, 10:59 AM)ciadmin Wrote: See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#resource-routes

Brilliant! Thanks!


RE: Do I need to set all my routes in Routes.php? - includebeer - 08-10-2019

(08-10-2019, 10:59 AM)ciadmin Wrote: See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#resource-routes

Oh yes, I forgot about that! Cool


RE: Do I need to set all my routes in Routes.php? - titounnes - 08-10-2019

(08-10-2019, 09:18 AM)tgix Wrote:
(08-10-2019, 06:46 AM)includebeer Wrote: If you don’t want to configure all your routes, you can still use “auto-route” like in CI3: https://codeigniter4.github.io/userguide/incoming/routing.html#use-defined-routes-only

PHP Code:
$routes->setAutoRoute(true); 

OK. In CI3 I used chriskacerguis/codeigniter-restserver to handle GET, POST etc.
I guess auto routing will work with GET and I can setup only POST, OPTIONS etc for the special cases?
If you want to prevent requests with the get method, use filters


RE: Do I need to set all my routes in Routes.php? - tgix - 08-11-2019

(08-10-2019, 10:59 AM)ciadmin Wrote: See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#resource-routes

Just a question about the automatic routes created. Why did you add both edit() and update()? What am I missing?


RE: Do I need to set all my routes in Routes.php? - kilishan - 08-11-2019

The new and edit methods are intended to return forms, based on Ruby on Rails, IIRC.

While it might feel cumbersome at first to record every single route in the routes file, I can say from experience it becomes nice. It acts as documentation about what routes are in the system, making it simple to look up what controller/method is handling that route. It also provides a lot of extra features that you don't get just by auto-routing, including ensuring a route only handles the HTTP verb it's supposed to.


RE: Do I need to set all my routes in Routes.php? - tgix - 08-11-2019

Thanks for the explanation. I deal mainly with REST AJAX calls from the front-end so I don't do any views from CI4.


To handle missing a method I created an interface defining all methods set by a $routes->resources() call and then add the special ones as separate routes. For a decently-sized project, the route configuration gets rather extensive though and many opportunities to make a spelling mistake.