簡體   English   中英

在不同的用戶角色上使用redirectUrl()的CakePHP 3.x

[英]CakePHP 3.x using redirectUrl() on diffrent User Roles

我正在使用身份驗證組件在登錄和注銷時設置重定向:

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

現在我想將不同的角色重定向到請求的鏈接。為此,我正在使用:

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

它僅適用於諸如視圖和編輯之類的請求,否則它會退回到Auth Component redirect,我不想要。

當請求視圖且尚未登錄用戶/管理員時,我如何才能完成在身份驗證組件(應用控制器)的角色切換器和重定向中進行常規回退的操作。

為此,我嘗試:

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

 }

哪個行不通,任何想法都將不勝感激。

$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

您可以使用$this->Auth->user()檢查登錄的用戶

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM