Welcome Guest, Not a member yet? Register   Sign In
Do I need to set all my routes in Routes.php?
#1

(This post was last modified: 08-10-2019, 12:59 AM by tgix. Edit Reason: typo )

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); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
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
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
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
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.
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




Theme © iAndrew 2016 - Forum software by © MyBB