问题
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