如何在应用程序控制器中配置身份验证组件


How to configure Auth component in appcontroller

我正在使用cakephp 2.6。

我遵循了 cakephp 食谱中的教程,发现有两种方法可以在应用程序控制器中配置身份验证组件。

第一个是:

public $components = array(
  'Acl',
  'Auth' => array('authorize' => array('Actions' => array('actionPath' => 'controllers'))),
  'Session'
);

另一个是:

public $components = array(
  'Acl',
  'Auth' => array('authorize' => 'Controller'),
  'Session'
);

所以我的问题是:它们之间有什么区别,为什么我们应该使用授权参数?

试试这个

public $components = array(
    'Cookie',
    'Email',
    'RequestHandler',
    'Session' ,
    'Auth'=>array(
        'loginAction' => '/login',
        'loginRedirect'=> '/login', //~ where to redirect if user not login
        'logoutRedirect'=> '/logout',
        'authError'=>"You can't access this page",
        'authenticate' => array('Form',array('fields' => array('username' => 'email'),'userModel' => 'User')), //~ user email as username if login with email else use username in case of log
        'authorize'=>array('Controller')    
    ) ,
    'RequestHandler',
    'Upload'
);

将此功能放在应用程序控制器中

public function isAuthorized($user) {
    if($user)
        return true;
    else
        return false;
}

您可以在登录功能中使用以下代码登录

$this->Auth->login();