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

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?
Code:
$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');

versus
Code:
$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?".
Thanks
Reply
#2

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 )
Reply
#3

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

@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?
Reply
#5

It is better not to use (:any) unless you need to catch multiple URI segments.
https://codeigniter4.github.io/CodeIgnit...ior-of-any

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

Some examples for GET :

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

An example for POST:

PHP Code:
$routes->post('update_user_settings','SiteUserPanel::update_user_settings'); 
Reply
#7

(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:
$routes->match(['get','head'
...and use 
PHP Code:
$routes->post 
for POST routes
Reply
#8

Separate is better
Reply
#9

You're better off separating it!
Reply
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB