1

最近我将cakephp 3.3.4升级到cakephp 3.4.7,在旧版本中如果在当前页面登录它只会重定向到当前页面,但升级后它会重定向到主页,我不知道是什么问题。请任何人在这里帮助我

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'

        ]
    ]);
}

登录功能:

public function login()
{
    if($this->Auth->user()){
        $this->Flash->error(__('You are already logged in!'));
        return $this->redirect($this->Auth->redirectUrl());
    }
    else{
        if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error('Your username or password is incorrect.');
      }
   }
}
4

1 回答 1

0

重定向到当前页面:

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

如果您尝试在登录时重定向到特定位置,请确保在 Appcontroller 中初始化 Auth 组件时设置了 loginRedirect。

public function initialize()
{
    parent::initialize();

    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login',
            'prefix' => false
        ],
        'unauthorizedRedirect' => $this->referer(),
         /* You need this part */
        'loginRedirect' => [
            'controller' => 'dashboard',
            'action' => 'index'
         ]
    ]);

}

来源:https ://book.cakephp.org/3.0/en/controllers/components/authentication.html#redirecting-users-after-login

于 2017-05-24T07:14:39.227 回答