我试图根据官方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/这样的子文件夹中,example.com/project1/users/login 正确的完整 url,但是当尝试访问 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"?
我发现了这个问题。
我根据@ndm的链接更改了代码:
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => CakeRoutingRouter::url('/users/login'),
'queryParam' => 'redirect',
导致无限重定向,因为我忘记将此功能添加到用户控制器:
public function beforeFilter(CakeEventEventInterface $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' => CakeRoutingRouter::url('users/login'),
在此之后它对我有用