2016-11-20 77 views
0

我使用的驗證組件上設置登錄重定向與註銷: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想法,將不勝感激。

回答

2
$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