cakePHP 3.4 中的登录重定向

Login redirecting in cakePHP 3.4

我正在尝试使用 cakephp 3.4 登录后重定向到当前页面,但我遇到了这样的问题

localhost page isn't working, locahost page redirecting you too many times. Try clearing your cookies

2 秒后重定向到主页。请帮帮我。 这是我的代码

在appController.php

public function initialize()
{ 
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize' => ['Controller'],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ],
                'scope' => ['userStatus' => '1']
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'unauthorizedRedirect' => $this->referer(),
        'logoutRedirect'       => [
                'controller' => 'Users',
                'action'     => 'login'
        ]
    ]);
}

在loginController.php

function login{ 
 if ( $this->request->is( 'post' ) ) {
    if ( $this->Auth->login() ) 
    {
       $this->redirect($this->referer());
    } 
    else {
      $this->Flash->error(__('Your username or password is incorrect.'));
    }
  }
}

看起来你这里有一些重定向循环。你应该使用 AuthComponent::redirectUrl().

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();

        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->error(__('Username or password is incorrect'));
        }
    }
}

请参阅文档中的 Redirecting Users After Login

After logging a user in, you’ll generally want to redirect them back to where they came from. Pass a URL in to set the destination a user should be redirected to after logging in.

If no parameter is passed, the returned URL will use the following rules:

  • Returns the normalized URL from the redirect query string value if it is present and for the same domain the current app is running on. Before 3.4.0, the Auth.redirect session value was used.
  • If there is no query string/session value and there is a config loginRedirect, the loginRedirect value is returned.
  • If there is no redirect value and no loginRedirect, / is returned.

使用 $this->Auth->redirectUrl() 代替 $this->referer()

用户登录后,您通常希望将他们重定向回他们来自的地方。传入 URL 以设置用户登录后应重定向到的目的地。

  • Returns 来自重定向查询字符串值的规范化 URL(如果存在)并且对于当前应用 运行 所在的同一域。在 3.4.0 之前,使用 Auth.redirect 会话值。
  • 如果没有query string/session值,有config loginRedirect,则返回loginRedirect值。
  • 如果没有重定向值且没有 loginRedirect,则返回 /

添加到您的 AuthComponent 配置选项:

loginRedirect

The URL (defined as a string or array) to the controller action users should be redirected to after logging in. This value will be ignored if the user has an Auth.redirect value in their session.

你的代码应该是这样的:

在appController.php

public function initialize()
{ 
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize' => ['Controller'],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ],
                'scope' => ['userStatus' => '1']
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'unauthorizedRedirect' => $this->referer(),
        'logoutRedirect'       => [
                'controller' => 'Users',
                'action'     => 'login'
        ],
        'loginRedirect' => [
            'controller' => 'Pages',
            'action' => 'display'     
        ]
    ]);
}

在loginController.php

function login{ 
 if ( $this->request->is( 'post' ) ) {
    if ( $this->Auth->login() ) 
    {
       $this->redirect($this->Auth->redirectUrl());
    } 
    else {
      $this->Flash->error(__('Your username or password is incorrect.'));
    }
  }
}

另见 Redirecting Users After Login