CakePHP的3.x的

问题描述:

我使用的验证组件上设置登录重定向与注销:CakePHP的3.x的

$this->loadComponent('Auth', [ 
      'loginRedirect' => [ 
       'controller' => 'Adresses', 
       'action' => 'index' 
      ], 
      'logoutRedirect' => [ 
       'controller' => 'Pages', 
       'action' => 'display', 
       'home' 
      ], 
       'authenticate' => [ 
        'Form' => [ 
         'fields' => ['username' => 'email'] 
        ] 
       ], 
      'authorize' => 'Controller', 

     ]); 

现在,我希望用户得到不同的页面上按照重定向他们的作用,迄今运作良好:

if ($this->Auth->user('role') == 'admin') {       
          return $this->redirect([ 
           'controller' => 'adresses', 
           'action' => 'adminindex' 
          ]); 
         } 
        } elseif ($this->Auth->user('role') == 'user') { 
         return $this->redirect([ 
          'controller' => 'adresses', 
          'action' => 'index' 
         ]); 
        } 

现在我想不同的角色重定向到所请求的links.And对我使用:

return $this->redirect($this->Auth->redirectUrl()); 

它只适用于查看和编辑等请求。否则它会回落到Auth Component重定向,这是我不想要的。

如何在Auth组件(应用程序控制器)我的角色切换器中执行常规回退,以及在请求视图并且用户/管理员尚未登录时重定向。

对于我尝试:

if(!empty($this->Auth->redirectUrl())){ 

} 

这并不working.Any想法,将不胜感激。

$this->Auth->redirectUrl() 

只是为将用户重定向后登录

看到http://book.cakephp.org/3.0/en/controllers/components/authentication.html#redirecting-users-after-login

,如果您需要重定向到引用者页面,则可以使用Controller::referer()

看到http://api.cakephp.org/3.3/class-Cake.Controller.Controller.html#_referer

可以用检查登录的用户

+0

非常感谢!像魅力一样工作(http://book.cakephp.org/3.0/en/controllers.html#redirecting-to-other-pages) – Isengo