CodeIgniter Forums
use sub-folders for controllers, distinguish folders from controller files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: use sub-folders for controllers, distinguish folders from controller files (/showthread.php?tid=76611)



use sub-folders for controllers, distinguish folders from controller files - mike433 - 06-01-2020

Hi
I followed the steps in the url below:

codeigniter.com/userguide3/general/controllers.html#organizing-your-controllers-into-sub-directories

and placed my controller "Test.php" inside a folder named "admin"  =>
Now when I type:

localhost/index.php/admin/test/ => it just works Smile , I didn't touch the routes.php and didn't do anything with the .htaccess

My question is: how does codeigniter recognizes "admin" is a folder and "test" is the controller? how come it doesn't understand them as "admin" is a controller, and "test" is a function?

I tried to follow the code starting from "index.php" and the files quickly below up so here I am hoping to get some clarification. I attached a code snippet from codeigniter.php where I think some sort of test is being done to check if a file exists, but I hope go get further clarification from you guys.

   


RE: use sub-folders for controllers, distinguish folders from controller files - jreklund - 06-01-2020

Hi, here are the complete path.

index.php -> system/core/CodeIgniter.php -> system/core/Router.php

__construct() -> _set_routing() -> _parse_routes() -> _set_request() -> _validate_request()

In _validate_request it tests if that's a directory or not.


RE: use sub-folders for controllers, distinguish folders from controller files - mike433 - 06-02-2020

(06-01-2020, 01:48 PM)jreklund Wrote: Hi, here are the complete path.

index.php -> system/core/CodeIgniter.php -> system/core/Router.php

__construct() -> _set_routing() -> _parse_routes() -> _set_request() -> _validate_request()

In _validate_request it tests if that's a directory or not.

This answer is SPOT ON. Thank you Sir. (__construct() -> _set_routing() -> _parse_routes() -> _set_request() -> _validate_request() where did you get that sequence from? it is not in the documentation Smile )



Now one final question: when I placed the default controller "Home" in the new sub-folder "site", and typed:
localhost/site/home   ==> things just work Smile .

But when I type:
localhost/site/   ==> it doesnot work Sad

I tried: $route['default_controller'] = 'Home'; ==> this didn't work
So I changed it to $route['default_controller'] = "site/Home"; ==> still didn't work.
How should I set the default-controller in this case?

Thx a ton again.


RE: use sub-folders for controllers, distinguish folders from controller files - jreklund - 06-02-2020

Hi, I just read the source code from start to finish. It's not documented as that's not normally what people care about.

I'm not sure that CI3 support multiple home controllers as CI4 does. According to the docs, you can't move it into a folder:
https://codeigniter.com/userguide3/general/routing.html#reserved-routes

But maybe you can trick it with "site/Home/index" as it thinks that "site" are your controller and "home" are your method at the moment.
__________________

If you now visit "localhost/" it won't display anything, as you just moved the default controller.
So it's better to add a specific route instead:
$route['site'] = 'site/home/index';


RE: use sub-folders for controllers, distinguish folders from controller files - mike433 - 06-03-2020

(06-02-2020, 11:00 AM)jreklund Wrote: Hi, I just read the source code from start to finish. It's not documented as that's not normally what people care about.

I'm not sure that CI3 support multiple home controllers as CI4 does. According to the docs, you can't move it into a folder:
https://codeigniter.com/userguide3/general/routing.html#reserved-routes

But maybe you can trick it with "site/Home/index" as it thinks that "site" are your controller and "home" are your method at the moment.
__________________

If you now visit "localhost/" it won't display anything, as you just moved the default controller.
So it's better to add a specific route instead:
$route['site'] = 'site/home/index';
Thanks a ton Smile