![]() |
Use of subfolder for controllers when using presenter or resources routes - 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: Use of subfolder for controllers when using presenter or resources routes (/showthread.php?tid=82658) |
Use of subfolder for controllers when using presenter or resources routes - gfgrisales - 08-04-2022 I've been looking over and over on Google, Forums and StackOverflow and so far I don't find an answer about this. I'm trying to maintain my code and files organized (and to not have to invent again the wheel) by using persenter and resources routes but I stumble upon the fact that I can't use Sub-Folders for my controllers when I do so. For the case: I have this routes defined: PHP Code: $routes->group('dashboard', static function ($routes) { and I intended to have the movie.php (controller file) on a sub-folder inside the controllers folder, something like this: Code: root But this is giving me an exception: Quote:404 - File Not Found Which is Basically telling me to move the Movie.php controller to the controllers root folder where it works like a charm. ![]() I read the RESTful Resource Handling manual on the official documentation as well but couldn't find anything that mention this. Is this the intended behaviour or is it a bug to be reported? Is there a way to use the controllers on a sub-folder when using resource or presenter routing? ![]() RE: Use of subfolder for controllers when using presenter or resources routes - kenjis - 08-05-2022 You can check your routes with spark routes command: Code: $ php spark routes (08-04-2022, 05:42 PM)gfgrisales Wrote: Is this the intended behaviour or is it a bug to be reported? I think it is the intended behavior. There is no relation with a route and a controller sub-directory. $routes->group('dashboard', ...) just adds dashboard in the route. It does not change the Controller of the presenter. If you want to change the controller, you need to specify the namespace. PHP Code: $routes->group('dashboard', ['namespace' => 'App\Controllers\dashboard'], static function ($routes) { RE: Use of subfolder for controllers when using presenter or resources routes - iRedds - 08-05-2022 The fact that after moving the Movie.php file to the controllers directory it starts working normally means that the class namespace is incorrectly defined. Let's start with the theoretical part. A namespace always has its own root directory. For example, in CI4, the App namespace has the root directory app/. All namespace segments must correspond to directories within the root directory of the namespace. For example namespace App -> app/ (root) namespace App\Controllers = app/Controllers or namespace Acme -> modules/acme/ (root) namespace Acme\Controllers = modules/acme/Controllers That is, the namespace defines the directory in which the class is located. Let's get back to your problem. 1. For a Movie class located in the app/Controllers/dashboard directory, the namespace should be App\Controllers\dashboard (note the namespace is case sensitive). 2. By default, the namespace is defined as App\Controllers. This helps shorten the handler name. PHP Code: $routes->get('/', 'Home::index'); That is, you only need to define the namespace. There are two ways. 1. Define the namespace for the group. PHP Code: $routes->group('dashboard', ['namespace' => '\App\Controllers\dashboard'], static function ($routes) { 2. Define controller PHP Code: $routes->group('dashboard', static function ($routes) { |