Controller Suffix |
Can someone help me, please;
I want to set every controller name with '_controller' at end file name and controller class name example: PHP Code: /** replace: PHP Code: /** but in url still like this: http://localhost:8000/welcome without _controller
My question would be... why? Aren't the controllers already inside the controllers directory?
Website: http://avenir.ro
I do suffix my models with _model, libraries with _library and views with _view which helps with my editor that sometimes gets confused if I have multiple file names with the same name. I would never suffix a controller though, since this is a url issue (unless overridden with routes of course).
CI does not remove anything from the controller name, so if you class is Foo_controller, then that is the only name that will work, calling Foo will not be recognized. So somewhere, if 'welcome' in your url is still working, CI must either be finding a controller for it or finding a route for it. Hope that helps, Paul.
@ufhy
Time ago I have taken a solution from here: http://code.tutsplus.com/tutorials/6-cod...--net-8308 But this must be adapted carefully for CodeIgniter 3.
Hi. Add the following lines to /application/config/routes.php file:
PHP Code: $route['welcome/(:any)'] = 'welcome_controller/$1'; Also read more about URI routing in the documentation: http://www.codeigniter.com/user_guide/ge...uting.html. (08-08-2016, 11:59 AM)Muzikant Wrote: Hi. Add the following lines to /application/config/routes.php file: Thats a great answer. But it still does not answer why you would want _controller on the end of every controller. And you would have to write those route pairs for every controller just to get around it. (08-05-2016, 08:56 AM)ufhy Wrote: Can someone help me, please; the simplest way is to do it with .htaccess mod_rewrite ![]()
God Bless CI Contributors
![]()
(08-08-2016, 01:56 PM)PaulD Wrote:(08-08-2016, 11:59 AM)Muzikant Wrote: Hi. Add the following lines to /application/config/routes.php file: Writing out your routes is a good practice ![]()
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Is it really? I am not sure myself, but I would certainly bow to greater knowledge.
Here is an example. A customer emails: Quote:Found an error on www.somesite.com/reports where link doesn't work on custom report. I know immediately that it is the reports controller. I go there and see some function PHP Code: $this->reports_model->get link() and I know immediately that is a call to the reports model function get_link. I check in there and find the error. The alternative is: A customer emails: Quote:Found an error on www.somesite.com/reports where link doesn't work on custom report. I have to check the routes first for where that link is routed to. With hundreds of routes that might not be such a simple taks. I find the route and go there and see some function PHP Code: $this->reports->get link() Is it an internal function? I check the controller and it is not. Is it a library, I check the library and in the reports_library I find a get_link function. I cannot find an error with that and scratch my head for a while. Then I discover it is a model call, so I check the model and find another get_link function and there is the error finally. Personally I stick to the magic routes for CI! Admittedly the example above is completely fictitious. So in using a framework that provides a routing methodology, is it really best practice to define every route? Best wishes, Paul
Your mindset is very common amongst devs who have never worked in larger teams, but its understandable. I had typed a large text below giving examples on why routes are important, but at the end of the day I think it's better to just copy something off of wikipedia:
"In computer science, separation of concerns (SoC) is a design principle for separating a computer program into distinct sections, such that each section addresses a separate concern." https://en.wikipedia.org/wiki/Separation_of_concerns By using automagical routing, your controllers are directly tied to the URL. If down the road you need to change a URL, you're now forced to change the name of your controller. On top of separating your URL's from your controllers, as soon as you start working in a team, you'll find that your Routes.php file will be a perfect 'map' of your application. Like you said before, you have to hit the urls to see what's going on. With a fully mapped routes.php, your other developer could just look at the routes file and with a simple ctrl+F + 'reports', you can see every function that should be available to your users quickly. Just to add, using routes also allows you to have urls like "users/1234/posts/2/edit" which can help simplify your front-end if you're used to working with rest styled interfaces. At the end of the day, there's a reason why most modern frameworks used named routes and CI4 will now have the option to turn off magical routing. While it makes it easier to get started, it's not the best practice if you're building any app with more than just a few controllers.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
|
Welcome Guest, Not a member yet? Register Sign In |