2016-08-02 73 views
1

我正在cakephp項目上工作。我有問題,但不知道如何解決它。 我在Session調用中有變量存儲是'角色'。而且我有一些路線由這個角色管理,但是被其他角色拒絕。那麼我怎麼能通過這樣的角色來配置路由。給我一個提示。謝謝你這麼多按角色管理路由cakephp

樣品也許我想這樣

if($this->Session->read("role")=="admin"){ 
    allow("/admin/dashboard"); 
}else{ 
    denied("/admin/dashboard"); 
} 

if($this->Session->read("role")=="staff"){ 
    allow("/staff/dashboard"); 
} 

回答

0

ELLO,夥計。

您是否使用Auth組件對登錄進行身份驗證?如果你使用它,你可以允許和拒絕與控制器操作:

//AdminController 
$this->Auth->allow('dashboard'); //Allow the dashboard method on admin controller 
$this->Auth->deny('dashboard'); //Deny the dashboard method on admin controller 

您也可以在beforeFilter()的基礎上的作用,其重定向到條件:

//ExampleController 
public function beforeFilter() 
{ 
    if($this->Session->read("role")=="admin") 
    { 
     return $this->redirect(array('controller' => 'yo_controller', 'action' => 'yo_action')); 
    } 
    //....... 
} 

如果你想,看看cookbook關於授權,它可能會幫助你。