CakePHP5 Named route not found

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