Welcome Guest, Not a member yet? Register   Sign In
Routes: Which style is "better"?

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?".

Personaly I perfer to use the single get & post. This keeps them seperate.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

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.

@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?

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.

Some examples for GET :

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

An example for POST:

PHP Code:

(This post was last modified: 12-29-2023, 05:19 AM by lazcorp.)

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

Separate is better

You're better off separating it!

Theme © iAndrew 2016 - Forum software by © MyBB