CakePHP5 Named route not found

Hello everyone,
first thank you for taking time read my post and trying to help me.

I am new in CakePHP and still learning. Worked with another framework before but this is stropped from beeing supprted and I will learn to reprogram my sites in CakePHP.

I started with the tutorial and now I like to improve it to send a user to a forbidden page when he/she is not authorized. The Authorization plugin is workind and a ForbiddenException is thrown correctly. So far all good.

In my config/routes.php I am using the following:

    $routes->setRouteClass(DashedRoute::class);
    $routes->scope('/', function (RouteBuilder $builder): void {

        // ... some other routes from tutorial

        $builder->connect(
            '/forbidden',
            ['controller' => 'Staticpages', 'action' => 'forbidden'],
            ['_name' => 'forbidden']
        );

        $builder->fallbacks();
    });

in my Application.php I call the named route by using this:

    public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
    {
        $middlewareQueue

            // ... many other middleware by tutorial

            ->add(new AuthorizationMiddleware($this, [
                'unauthorizedHandler' => [
                    'className' => 'Authorization.CakeRedirect',
                    'url' => Router::url(['_name' => 'forbidden']),
                    'queryParam' => 'redirectUrl',
                    'exceptions' => [
                        MissingIdentityException::class,
                        ForbiddenException::class,
                    ],
                ],
            ]));


        return $middlewareQueue;
    }

When I am accessing the page, dont matter wich site, the framework always errors with:

Missing Route
A route matching forbidden could not be found.

When using bin\cake routes (I am using Windows) the route is listed in the table.

I am trying to understand what is going for the last 2 days but I cant find any error.

Thanks for your help!

Hello everyone,

I got it working now. In another post from Nov 2023 here I found out, this is a timing problem and I misunderstand the manual.

In manual is written, that the CakeRedirect handler supports CakePHP Router syntax.

But the problem is:

No routes are being loaded until the routing middleware runs, so you cannot use the router at that point,
Authentication and Authorization plugin - Need Help - Cake Software Foundation, Inc. (cakephp.org)

So I changed my code to syntax of the Router which sompletelyx solved my problem.

In my Application.php I call the named route using this:

    public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
    {
        $middlewareQueue

            // ... many other middleware by tutorial

            ->add(new AuthorizationMiddleware($this, [
                'unauthorizedHandler' => [
                    'className' => 'Authorization.CakeRedirect',
                    'url' => (['_name' => 'forbidden']),
                    'queryParam' => 'redirectUrl',
                    'exceptions' => [
                        MissingIdentityException::class,
                        ForbiddenException::class,
                    ],
                ],
            ]));


        return $middlewareQueue;
    }

I posted this so anybody else maybe will read it so it will help him/her.
Thank you, have a nice day.

1 Like

you don’t need to flag your own post :wink:

The fact that you posted the answer is more than enough and just leave this post be as it is.

Maybe someone in the future will encounter the same problem - therefore this doesn’t need to be deleted.