使用cakephp 3.4.7登录后重定向到当前页面

使用cakephp 3.4.7登录后重定向到当前页面,cakephp,cakephp-3.0,cakephp-3.4,Cakephp,Cakephp 3.0,Cakephp 3.4,最近我将cakephp 3.3.4升级到cakephp 3.4.7,在旧版本中,如果当前页面中有loggedin,它只会重定向到当前页面作为我的方面,但升级后它会重定向到主页,我不知道是什么问题。请有人帮帮我吗 public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash');

最近我将cakephp 3.3.4升级到cakephp 3.4.7,在旧版本中,如果当前页面中有loggedin,它只会重定向到当前页面作为我的方面,但升级后它会重定向到主页,我不知道是什么问题。请有人帮帮我吗

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.');
      }
   }
}

重定向到当前页面:

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组件时设置了LoginDirect

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'
         ]
    ]);

}

来源:

重定向到当前页面:

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组件时设置了LoginDirect

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'
         ]
    ]);

}

来源:

Hi,我使用的是你发布的内容,但仍然只是重定向到主页。您可以在问题中看到我的代码。从3.1开始,作用域和包含选项已被弃用。改为使用自定义查找器修改查询以获取用户记录。您是否包含了
$this->redirect($this->Auth->redirectUrl())在您的注销操作中?e、 g
public function logout(){$this->redirect($this->Auth->logout());
no它应该在登录函数中不,在注销函数中我正在使用这个返回值$this->redirect($this->Auth->logout());你能确认一下
this->redirect($this->->Auth->redirectUrl())
是否在您的登录操作中?是的,这里是我的代码
公共函数login(){if($this->Auth->user()){$this->Flash->error(uuu('您已登录!'));返回$this->redirect($this->Auth->redirectUrl());}否则{if($this->request->is('post')){$user=$this->Auth->identify();if($user){$this->Auth->setUser($user);返回$this->redirect($this->Auth->redirectUrl());}$this->Flash->error('您的用户名或密码不正确。');}}}
您好,我正在使用您发布的内容,但它仍然只重定向到主页。您可以查看我的问题代码范围和包含选项从3.1开始就已被弃用。请使用自定义查找器来修改查询以获取用户记录。是否包含了
$this->redirect($this->Auth->redirectUrl())
在您的注销操作中?例如
公共函数logout(){$this->redirect($this->Auth->logout());
不应该在登录函数中不,在注销函数中我正在使用这个返回$this->redirect($this->Auth->logout());您能确认一下
$this->redirect($this->->Auth->redirectUrl())
是否在您的登录操作中?是的,这里是我的代码
公共函数login(){if($this->Auth->user()){$this->Flash->error(uuu('您已登录!'));返回$this->redirect($this->Auth->redirectUrl());}否则{if($this->request->is('post')){$user=$this->Auth->identify();if($user){$this->Auth->setUser($user);返回$this->redirect($this->Auth->redirectUrl());}$this->Flash->error('您的用户名或密码不正确。');}}}
相关问题:-,您可以在此处找到答案相关问题:-,您可以在此处找到答案