2015-01-09 55 views
0

我正在使用cakephp 2.6。如何在應用程序控制器中配置Auth組件

我遵循cakephp的食譜中的教程,發現在appcontroller中有兩種配置Auth組件的方法。

第一個是:

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

,另一個是:

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

所以我的問題是:什麼是它們之間的區別,以及爲什麼我們應該使用授權參數?

回答

0

試試這個

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(); 
+0

感謝期運用下面的代碼登錄功能,爲您的答案,但基本上我想知道爲什麼我應該使用**'authorize'=> array('Controller')的授權參數** –

+1

你會在這裏找到你的答案。 http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization –

相關問題