CakePHP 4 CMS身份验证教程重定向到登录无效(忽略子文件夹)

时间:2020-05-26 19:02:37

标签: cakephp cakephp-4.x

我正在尝试根据官方CMS教程https://book.cakephp.org/4/en/tutorials-and-examples/cms/authentication.html#adding-login

进行身份验证

但是重定向在此处实现:

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

不能按预期工作。

“我的安装”位于example.com/project1/之类的子文件夹中,正确的完整URL为example.com/project1/users/login,但是当尝试访问example.com/project1/时,重定向指向example.com / users / login。

我也尝试过改变

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

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

但这会导致

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

错误

如何设置重定向或在CakePHP 4中可以在哪里更改“ BASEURL”?

2 个答案:

答案 0 :(得分:2)

我找到了问题。

我将代码according to @ndm's link更改为此:

$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']);
}

答案 1 :(得分:0)

我有同样的问题。 我也不得不改变

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

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

在此之后它对我有用