Routing to plugin using prefix error: Missing Controller

Hi

I got “Missing Controller” though the controller class seems to be found by Router and the controller exits.
When the url is /admin/stats/contenus, i want to reach the action=>contenus in the StatistiquesController.php in my plugin=>Statistics using prefix=>admin.
In Application::bootstrap
$this->addPlugin(‘Statistics’);

In my main project route.php:

    $routes->prefix('admin', function (RouteBuilder $builder) {
        .../...
        /* Option 1 */
        $builder->connect('/stats/*', ['plugin' => 'Statistics', 'controller' => 'Statistiques', 'prefix' => false]);

        $builder->fallbacks(DashedRoute::class);
    });

    /* Option 2 */
    $routes->prefix('/admin/stats', function (RouteBuilder $builder) {
        $builder->connect('/*', ['plugin' => 'Statistics', 'controller' => 'Statistiques', 'prefix' => false]);
    });

    /* Option 3 */
    $routes->scope('/admin', function (RouteBuilder $builder) {
        $builder->connect('/stats/*', ['plugin' => 'Statistics', 'controller' => 'Statistiques', 'prefix' => false]);
    });

In my plugins/Statistics/src/Controller/StatistiquesController.php

    <?php
    declare(strict_types=1);
    namespace Statistics\Controller;
    use Statistics\Controller\AppController;
    class StatistiquesController extends AppController
    {
        public function contenus(): void
        {
        }
    }

None of the options above work.
When I type following url “http://localhost:8765/admin/stats/contenus
I get the following error:
Create the class StatistiquesController below in file: W:\WORK-CK4/plugins/Statistics/src\Controller\StatistiquesController.php

My controller exists in the location and has the right name.
What am I missing?

Thx for help.

i want to reach the action=>contenus in the StatistiquesController.php in my plugin=>Statistics using prefix=>admin.

Then why are you using 'prefix' => false when connecting the routes?

Also if you want to use admin as prefix then your controller need to be under the namespace namespace Statistics\Controller\Admin;.

1 Like

Thx for answer

This is not exactly what I want (forget about prefix).
my url: /admin/stats/contenus
should fire ==> plugin=Statistics, controller=Statistiques, action=contenus
because I already use admin for the authenticated part of my app.

I created the suggested controller at the right location with the right namespace with the suggested code (that is exactly what I expected) but the controller can’t be found!

The error dump shows
object(Cake\Http\ServerRequest) id:0 {

This seems to be what I want, but the controller is missing…

All the tries I made for route.php lead to the same error and the same suggestion.

I’ve seen the same issue on Stack Overflow ( php - CakePHP 4 can’t load plugin’s controller - Stack Overflow )
but nobody has answer since 1,5 year.

First option should work but you need to remove 'prefix' => false . Tested on my local env and it worked using a plugin. And each time i added 'prefix' => false gave the same error you have