9阅网

您现在的位置是:首页 > 知识 > 正文

知识

cakephp - CakePHP4CMS认证教程重定向登录不成功(子文件夹忽略)。

admin2022-10-28知识16

我想根据官方的CMS教程实现一个认证。https:/book.cakephp.org4entutorials-and-examplescmsauthentication.html#adding-login。

但这里实现的重定向。

public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
    $authenticationService = new AuthenticationService([
        'unauthenticatedRedirect' => '/users/login',
        'queryParam' => 'redirect',
    ]); 

不像预期的那样工作。

我的安装在一个子文件夹中,比如example.comroject1,正确的完整网址是example.comroject1userslogin,但是当试图到达example.comroject1时,重定向指向example.comuserslogin。

我也尝试改变

$authenticationService = new AuthenticationService([
        'unauthenticatedRedirect' => '/users/login',
        'queryParam' => 'redirect',

$authenticationService = new AuthenticationService([
        'unauthenticatedRedirect' => [controller => 'users', 'action' => index],
        'queryParam' => 'redirect',

但这将导致

parse_url()期望参数1是字符串,数组给定。

错误

CakePHP4中的 "BASEURL "要怎么设置重定向或者在哪里可以修改?



【回答】:

我找到了问题。

我修改了代码 根据@ndm的链接 到这个函数。

$authenticationService = new AuthenticationService([
        'unauthenticatedRedirect' => \Cake\Routing\Router::url('/users/login'),
        'queryParam' => 'redirect',

导致无限重定向,因为我忘了在UsersController中添加这个功能。

public function beforeFilter(\Cake\Event\EventInterface $event)
{
    parent::beforeFilter($event);
    // Configure the login action to not require authentication, preventing
    // the infinite redirect loop issue
    $this->Authentication->addUnauthenticatedActions(['login']);
}
【回答】:

我也遇到了同样的问题。

'loginUrl' => ('/users/login'),

'loginUrl' => \Cake\Routing\Router::url('users/login'),

在这之后,它为我工作