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? Code: $routes->match(['get', 'post'], 'lists/', 'Lists::index'); versus Code: $routes->match(['get', 'post'], 'lists/', 'Lists::index'); 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
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.
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.
Some examples for GET :
PHP Code: $routes->get('/{locale}/register', 'SiteUserPanel::register'); An example for POST: PHP Code: $routes->post('update_user_settings','SiteUserPanel::update_user_settings');
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'] PHP Code: $routes->post
I perfer to use the single get & post. This keeps them seperate. A hair band, or headband, is a flexible band worn around the head to hold hair in place or as a fashion accessory. hey come in various materials, widths, and designs, serving both practical and fashion purposes.
|
Welcome Guest, Not a member yet? Register Sign In |