Welcome Guest, Not a member yet? Register   Sign In
Use of subfolder for controllers when using presenter or resources routes

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:


But this is giving me an exception:

Quote:404 - File Not Found

Controller or its method is not found: \App\Controllers\Movie::index

Which is Basically telling me to move the Movie.php controller to the controllers root folder where it works like a charm.  Dodgy

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? Huh
Machines obbey the software instructions, software instructions obbeys the logic of the developer.
Therefore is not the systems fault if the developer's logic is fudged up!  ¯\_(ツ)_/¯

(This post was last modified: 08-05-2022, 05:37 PM by kenjis.)

You can check your routes with spark routes command:
$ 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) {
    $routes->presenter('category', ['except' => ['show']]);

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


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:
// instead of
Since you don't have a namespace defined in your routes, the default namespace is used, which is App\Controllers. Accordingly, the error indicates that the router is trying to find the Movie class in the app/Controllers directory.

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) {
// \App\Controllers\dashboard + Movie 

2. Define controller
PHP Code:
$routes->group('dashboard', static function ($routes) {
    $routes->presenter('movie', ['controller' => 'dashboard\Movie']);
    $routes->presenter('category',['controller' => 'dashboard\Category''except'=>['show']]);
// \App\Controllers +  dashboard\Movie 

Theme © iAndrew 2016 - Forum software by © MyBB