Should "Index" be avoided as a controller name in CodeIgniter? |
Is it recommended to avoid using "Index" as the name for a controller class in CodeIgniter? I've noticed that when I have an "Index" controller, its methods are called multiple times. Specifically, the index method is always called first, regardless of whether I visit a path that should be routed there.
Here's an example from the code: class Index extends CI_Controller { public function index() { echo "index"; } public function blah() { echo "blah"; } } When I visit "index/blah", I see "indexblah" printed. When I visit "index/index", I see "indexindex". However, if I rename the controller to something else (e.g., "Foo"), the issue doesn't occur. While renaming the controller is a workaround, I'm curious to know why this behavior is happening. Should I report this as a bug to CodeIgniter? (Note: I haven't set up any routes in "configs/routes.php," and my "index.php" is located developer echat outside the CodeIgniter directory.) I appreciate any insights or advice regarding this matter.
Yes index should not be used as a controller name.
Just think resevered words. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Call your default controller by another name, aside from “Index” in order to avoid conflicts with built-in routers. For instance, you may call it ”Home” or any other appropriate name.
In order to keep “Index” as a name of your controller ensure to state custom routes in the “config/routes.php” in regards to request route to your Index controller. This will help in defining particular routing rules for various URLs. Here's an example of how you can define a custom route in "config/routes.php" to handle "index/blah" and "index/index" differently: Code: $route['index/blah'] = 'Index/blah';
(11-05-2023, 11:58 AM)alexwriter Wrote: Call your default controller by another name, aside from “Index” in order to avoid conflicts with built-in routers. For instance, you may call it ”Home” or any other appropriate name.yes i understand, thanks a lot for your example
newbie coder
|
Welcome Guest, Not a member yet? Register Sign In |