• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do I need to set all my routes in Routes.php?

#1
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?
Reply

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

PHP Code:
$routes->setAutoRoute(true); 
Test your translation files with Translation Tester
Reply

#3
(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...outes-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?
Reply

#4
See https://codeigniter4.github.io/CodeIgnit...rce-routes
Reply

#5
(08-10-2019, 10:59 AM)ciadmin Wrote: See https://codeigniter4.github.io/CodeIgnit...rce-routes

Brilliant! Thanks!
Reply

#6
(08-10-2019, 10:59 AM)ciadmin Wrote: See https://codeigniter4.github.io/CodeIgnit...rce-routes

Oh yes, I forgot about that! Cool
Test your translation files with Translation Tester
Reply

#7
(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...outes-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
Reply

#8
(08-10-2019, 10:59 AM)ciadmin Wrote: See https://codeigniter4.github.io/CodeIgnit...rce-routes

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

#9
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.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#10
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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