CakePHP 4 CMS Authentication Tutorial redirection to login not working (subfolder ignored)

久未见 提交于 2020-06-29 04:40:06

问题


Im trying to implement an authentication according to the offical CMS Tutorial: https://book.cakephp.org/4/en/tutorials-and-examples/cms/authentication.html#adding-login

But the Redirection implemented here:

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

does not work as expected.

My Installation is in a subfolder like example.com/project1/ and the correct full url would be example.com/project1/users/login but when trying to reach example.com/project1/ the redirection points to example.com/users/login.

I also tried changing

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

to

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

but this results in an

parse_url() expects parameter 1 to be string, array given

error

How do I have to set the redirection or where can I change the "BASEURL" in CakePHP 4?


回答1:


I found the Issue.

I changed the code according to @ndm's link to this:

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

leading to an infinite redirect, because I had forgotten to add this function to the 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']);
}



回答2:


I had the same problem. I also had to change

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

to

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

After this it worked for me



来源:https://stackoverflow.com/questions/62029465/cakephp-4-cms-authentication-tutorial-redirection-to-login-not-working-subfolde

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!