Controllers under subfolder - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Controllers under subfolder (/showthread.php?tid=65608) |
Controllers under subfolder - fromberg - 06-30-2016 Hi, I have a controller under a sub-folder (Controllers\ajax\Test.php) but when I try to access from the browser (/ajax/test/testmethod) I get an error message "Controller or its method is not found." Please note if I do a class map in Config\Autoload.php like this: $classmap = [ "Test" => APPPATH . "Controllers/ajax/Test.php" ]; then it works. However, If I namespace my controller (namespace Ajax) then the controllers does not get loaded and get the same error message. Is there any workaround or should I do it in another way. Thanks in advance and best regards, Fabian RE: Controllers under subfolder - sv3tli0 - 06-30-2016
RE: Controllers under subfolder - InsiteFX - 07-01-2016 I played around with this the other night for awhile, this is what I came up with. Folder Structure: Controllers -- -- Admin -- -- -- -- AdminController.php -- -- Base -- -- -- -- BaseController.php -- -- Frontend -- -- -- -- FrontController.php -- -- Home.php BaseController.php PHP Code: <?php namespace App\Controllers\Base; AdminController.php PHP Code: <?php namespace App\Controllers\Admin; FrontController.php PHP Code: <?php namespace App\Controllers\Frontend; Home.php PHP Code: <?php namespace App\Controllers; Of course I'am not even sure if this is the correct way but it does work for me. This should get you started on doing sub-folders, I hope it helps. RE: Controllers under subfolder - titounnes - 07-01-2016 (07-01-2016, 04:08 AM)InsiteFX Wrote: I played around with this the other night for awhile, this is what I came up with.replace namespace App\Controllers\Base; with namespace Base; It's work for me. RE: Controllers under subfolder - arma7x - 07-01-2016 Why not put "MY_Controller" inside libraries folder and add namespace App\Libraries in "MY_Controller" then extend it to \CodeIgniter\Controller. Now to use it, extends all controller in controller folder to \App\Libraries\MY_Controller. RE: Controllers under subfolder - titounnes - 07-01-2016 (07-01-2016, 02:12 PM)arma7x Wrote: Why not put "MY_Controller" inside libraries folder and add namespace App\Libraries in "MY_Controller" then extend it to \CodeIgniter\Controller. Now to use it, extends all controller in controller folder to \App\Libraries\MY_Controller.i'm not sure, put My_Controller in libraries is good idea. Base controller is not a library. RE: Controllers under subfolder - fromberg - 07-01-2016 Hi All, thanks for your feedback and suggestions. I have tried all but still the same problem. I downloaded the latest CI 4 from repository. My environment is Windows. I will keep trying. If any additional tip, would be highly appreciated. Thanks and regards, RE: Controllers under subfolder - arma7x - 07-02-2016 (07-01-2016, 09:20 PM)titounnes Wrote:(07-01-2016, 02:12 PM)arma7x Wrote: Why not put "MY_Controller" inside libraries folder and add namespace App\Libraries in "MY_Controller" then extend it to \CodeIgniter\Controller. Now to use it, extends all controller in controller folder to \App\Libraries\MY_Controller.i'm not sure, put My_Controller in libraries is good idea. Base controller is not a library. You can put your own "MY_Controller" in any folder/create new one inside application folder as long as you give namespace to where it located. RE: Controllers under subfolder - sv3tli0 - 07-02-2016 Remember that MY_ classes existed in the old CI versions because there were no namespaces.. Now in CI 4 you are able simply to create App/Controllers/Controller and to use it from your other controllers.. The era of MY_ logic is over. RE: Controllers under subfolder - arma7x - 07-02-2016 (07-01-2016, 10:07 PM)fromberg Wrote: Hi All, This is controller file inside sub-folder Ajax in Controller folder; PHP Code: <?php namespace App\Controllers\Ajax; Edit your route file, /application/Config/Routes.php by adding: PHP Code: $routes->add('ajax/test', 'Test::index', ['namespace' => 'App\Controllers\Ajax']); It's works for me. Using CI4, if you namespace the controller, you have to set $routes->setDefaultNamespace('App\Controllers'); and define $routes->add() for each controller(Having 404NotFound problem if I’m did not do this). |