Cakephp Auth是否可以在其他控制器中使用?

Cakephp Auth是否可以在其他控制器中使用?,php,cakephp-2.0,Php,Cakephp 2.0,最近,我一直在研究cake,我看到据说是的auth库将负责对你的应用程序的访问控制,但是,看起来,当你不在“UsersController”中时,你不能初始化甚至使用这个auth库,我不想这样,如果它有一些管理部分,我希望URI是admin/login,那该怎么办,或者只是简单地/登录,我一直在挠头这个,请帮助 另一个问题,为什么当我将“$this->redirect”的功能放在任何只包含重定向的方法上,或者甚至放在_构造()中时,“$this->redirect”的功能似乎无效 谢谢各位,希望

最近,我一直在研究cake,我看到据说是的auth库将负责对你的应用程序的访问控制,但是,看起来,当你不在“UsersController”中时,你不能初始化甚至使用这个auth库,我不想这样,如果它有一些管理部分,我希望URI是admin/login,那该怎么办,或者只是简单地/登录,我一直在挠头这个,请帮助

另一个问题,为什么当我将“$this->redirect”的功能放在任何只包含重定向的方法上,或者甚至放在_构造()中时,“$this->redirect”的功能似乎无效


谢谢各位,希望有人能向我清楚地解释这些事情。

您可以在应用程序中的任何控制器中使用Auth组件。如果您希望它只对admin部分有效,那么您可以在应用程序AppController中的beforeFilter函数中添加条件,如身份验证初始化

// for component initialization.
public $components = array(
    'Auth' => array(
         'authenticate' => array(
            'userModel' => 'Customer', // you can also specify the differnt model instead of user
        'Form' => array(
        'fields' => array('username' => 'email')        
         )
      )
    )
}
您可以在管理员路由上绑定它,如

function beforeFilter(){        
    // only works with admin routing.
    if(isset($this->request->params['prefix']) && ($this->request->params['prefix'] == 'admin')){
        $this->Auth->loginRedirect  = array('admin' => true, 'controller' => 'pages', 'action' => 'index');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => true);
        $this->Auth->loginAction = array('admin' => true, 'controller' => 'customers', 'action' => 'login');
    }
}
如果您使用的是cake 2.3.x或更高版本,请确保已以正确的格式指定重定向操作,如

return $this->redirect('action_name'); // you can also specify the array of parameters.
类似的问题: