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

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');
// instead of
$routes->get('/''\App\Controllers\Home::index'); 
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) {
    $routes->presenter('movie');
    $routes->presenter('category',['except'=>['show']]);
}); 
// \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 
Reply


Messages In This Thread
RE: Use of subfolder for controllers when using presenter or resources routes - by iRedds - 08-05-2022, 06:28 PM



Theme © iAndrew 2016 - Forum software by © MyBB