按角色管理路线


Manage routes by roles cakephp

我正在进行cakepp项目。我有问题,但不知道如何解决。会话中的变量存储调用为"roles"。我有一些路由由这个角色管理,但被其他角色拒绝。那么,我该如何通过这样的角色来配置路由呢。给我一个提示。非常感谢

样品也许我想要像这个

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

Ello,伙计。

您是否使用身份验证组件对登录进行身份验证?如果您正在使用它,您可以使用允许和拒绝控制器操作

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

如果你愿意,可以看看关于授权的食谱,它可能会对你有所帮助。