CodeIgniter Forums
Routes: Which style is "better"? - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: Best Practices (
+--- Thread: Routes: Which style is "better"? (/showthread.php?tid=89043)

Routes: Which style is "better"? - SoccerGuy3 - 12-27-2023

Since explicit routing became the recommendation a while back, I have wondered about the style of the route. Is there a "best practice"? Or is just developer preference?
Running CI 4.3.3 if it matters.
To illustrate, let's assume we have some controller ("lists") with a home page, an add record, an edit record and a report (super simple example, not real world). Is one of these better than the other?
$routes->match(['get', 'post'], 'lists/', 'Lists::index');
$routes->match(['get', 'post'], 'lists/add/(:any)', 'Lists::add/$1');
$routes->match(['get', 'post'], 'lists/edit/(:any)', 'Lists::edit/$1');
$routes->match(['get', 'post'], 'lists/report/(:any)', 'Lists::report/$1');

$routes->match(['get', 'post'], 'lists/', 'Lists::index');
$routes->match(['get', 'post'], 'lists/(:any)', 'Lists::$1');

With the various controls in my current project and being explicit for each function/page (like the first example), my routes file is now almost 650 lines. It can get confusing at times. Some of that is for various user permissions levels, but still quite long and could be cut by 75% going the other way I imagine.
I guess the root question is, "Is it better to be explicit for security or some other reason or is simple ok?".

RE: Routes: Which style is "better"? - InsiteFX - 12-27-2023

Personaly I perfer to use the single get & post. This keeps them seperate.

RE: Routes: Which style is "better"? - demyr - 12-28-2023

I also keep them seperate. If I definitely need a page that must be both get and post, then I can use the match option. But, that's only for one page or two within a project, not for the all pages.

RE: Routes: Which style is "better"? - SoccerGuy3 - 12-28-2023

@demyr and @InsiteFX - I agree and pretty much do the single get/post as well, this was just a quick example to show off the difference in the URL handling. Which method do you use for that? The explicit or the more "wildcard" style?

RE: Routes: Which style is "better"? - kenjis - 12-28-2023

It is better not to use (:any) unless you need to catch multiple URI segments.

Generally it is better to register each route.
This is because it allows us to understand the specific routes.
You can use the php spark routes command to list all routes.

The match() method is generally not good.
If a single controller method performs the processing of multiple HTTP method requests,
the method becomes more complex.

RE: Routes: Which style is "better"? - demyr - 12-29-2023

Some examples for GET :

PHP Code:
$routes->get('/{locale}/user-panel/(:num)/dashboard''SiteUserPanel::user_dashboard', ['filter' => 'myLoginFilter']); 

An example for POST:

PHP Code:

RE: Routes: Which style is "better"? - lazcorp - 12-29-2023

I generally keep GET and POST routes as separate lines.
In my server logs I noticed quite a few failed HEAD requests (I wasn't providing any route for HEAD requests), so now I generally combine both GET and HEAD like this:
PHP Code:
...and useĀ 
PHP Code:
for POST routes

RE: Routes: Which style is "better"? - Derrick Vaillancourt - 01-10-2024

Separate is better

RE: Routes: Which style is "better"? - bekeanloise - 02-01-2024

You're better off separating it!