我正在尝试根据官方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”?
答案 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'),
在此之后它对我有用