Controllers under subfolder |
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
Best VPS Hosting : Digital Ocean
07-01-2016, 04:08 AM
(This post was last modified: 07-01-2016, 06:13 AM by InsiteFX. Edit Reason: Spelling error )
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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.
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.
Keep calm.
(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.
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, (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.
Keep calm.
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. Best VPS Hosting : Digital Ocean
(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).
Keep calm.
|
Welcome Guest, Not a member yet? Register Sign In |