CakePHP 4:CakePHP 路由 Url 缺少路由

CakePHP 4 : CakePHP Route Url Missing Route

我正在使用 authentication 插件登录 cakephp。

application.php , configAuth() 方法代码

protected function configAuth(): \Authentication\AuthenticationService
    {
        $authenticationService = new \Authentication\AuthenticationService([
            // 'unauthenticatedRedirect' => '/cake_invo/users/login' , //<= manually working fine
            'unauthenticatedRedirect' => \Cake\Routing\Router::url(['controller' => 'Users', 'action' => 'login']),
            'queryParam' => 'redirect',
        ]);

        // Load identifiers, ensure we check email and password fields
        $authenticationService->loadIdentifier('Authentication.Password', [
            'fields' => [
                'username' => 'email',
                'password' => 'password',
            ]
        ]);

        // Load the authenticators, you want session first
        $authenticationService->loadAuthenticator('Authentication.Session');
        // Configure form data check to pick email and password
        $authenticationService->loadAuthenticator('Authentication.Form', [
            'fields' => [
                'username' => 'email',
                'password' => 'password',
            ],
            // 'loginUrl' => '/cake_invo/users/login'  //<= manually working fine
            'loginUrl' => \Cake\Routing\Router::url(['controller' => 'Users', 'action' => 'login']),
        ]);

        return $authenticationService;
    }

中间件方法

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

            ->add(new ErrorHandlerMiddleware(Configure::read('Error')))

            ->add(new AssetMiddleware([
                'cacheTime' => Configure::read('Asset.cacheTime'),
            ]))

            ->add(new RoutingMiddleware($this))
            // add Authentication after RoutingMiddleware
            ->add(new \Authentication\Middleware\AuthenticationMiddleware($this->configAuth()));

        return $middlewareQueue;
    }

出现类似图片的错误

我该如何解决这个问题?

给出命令后bin/cake路由

问题是当您调用 Router::url() 时您的路由尚未加载,因此它会失败。

虽然中间件的顺序是正确的,即在路由中间件之后添加身份验证中间件,但您通过调用 $this->configAuth() 立即构建身份验证服务,这意味着 Router::url()调用将在任何中间件具有 运行 之前调用,特别是在路由中间件具有 运行 之前,它负责加载您的路由。

不要传递内置的身份验证服务实例,而是设置 as shown in the docs,即确保您的 Application class 实现 \Authentication\AuthenticationServiceProviderInterface,更改您的 configAuth 方法来匹配 AuthenticationServiceProviderInterface::getAuthenticationService(),然后将 $this 传递给身份验证中间件构造函数。这样该方法只会在身份验证中间件 运行 时被调用,即 路由中间件之后。

// ...
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Cake\Http\BaseApplication;
use Psr\Http\Message\ServerRequestInterface;

class Application extends BaseApplication implements AuthenticationServiceProviderInterface
{
    // ...

    public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
    {
        // ...
        return $authenticationService;
    }

    public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
    {
        $middlewareQueue
            // ...
            ->add(new RoutingMiddleware($this))
            ->add(new AuthenticationMiddleware($this));

        return $middlewareQueue;
    }

    // ...
}