• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Controller Suffix

#1
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:
/**
* Filename: application/Welcome.php
*/

Class Wecome extends CI_Controller {} 

replace:

PHP Code:
/**
* Filename: application/Welcome_controller.php
*/

Class Welcome_controller extends CI_Controller {} 

but in url still like this:
http://localhost:8000/welcome

without _controller
Reply

#2
My question would be... why? Aren't the controllers already inside the controllers directory?
Reply

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

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

#5
Hi. Add the following lines to /application/config/routes.php file:

PHP Code:
$route['welcome/(:any)'] = 'welcome_controller/$1';
$route['welcome'] = 'welcome_controller'

Also read more about URI routing in the documentation: http://www.codeigniter.com/user_guide/ge...uting.html.
Reply

#6
(08-08-2016, 11:59 AM)Muzikant Wrote: Hi. Add the following lines to /application/config/routes.php file:

PHP Code:
$route['welcome/(:any)'] = 'welcome_controller/$1';
$route['welcome'] = 'welcome_controller'

Also read more about URI routing in the documentation: http://www.codeigniter.com/user_guide/ge...uting.html.

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

#7
(08-05-2016, 08:56 AM)ufhy Wrote: 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:
/**
* Filename: application/Welcome.php
*/

Class Wecome extends CI_Controller {} 

replace:

PHP Code:
/**
* Filename: application/Welcome_controller.php
*/

Class Welcome_controller extends CI_Controller {} 

but in url still like this:
http://localhost:8000/welcome

without _controller

the simplest way is to do it with .htaccess mod_rewrite Smile
God Bless CI Contributors Smile
Reply

#8
(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:

PHP Code:
$route['welcome/(:any)'] = 'welcome_controller/$1';
$route['welcome'] = 'welcome_controller'

Also read more about URI routing in the documentation: http://www.codeigniter.com/user_guide/ge...uting.html.

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.

Writing out your routes is a good practice  Wink . Also, I'm sure it's a force of habit from someone coming from another framework. I do the same thing coming from a Ruby on Rails background.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply

#9
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
Reply

#10
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!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.