2013-10-11 52 views
0

最近,我一直在研究蛋糕,我已經看到了auth庫,它表示會照顧您的應用程序的訪問控制,但是,看起來像,您不能初始化,甚至不能使用此auth庫當你不在'UsersController'時,我不想要那個,如果它有一些管理員部分,我希望這個URI是admin/login,或者只是簡單地/登錄,我一直在撓頭一,請幫忙。Cakephp Auth可以在其他控制器中使用嗎?

另一個問題,爲什麼看起來像'$ this-> redirect'的功能在我把這個函數放在任何只包含重定向的方法中,甚至是在__construct()中都沒有效果?

謝謝你們,希望有人能夠清楚地向我解釋那些事情。

+0

類似的問題:http://stackoverflow.com/questions/16820195/cakephp-admin-section-routing-and-redirecting – Dezigo

回答

0

您可以使用應用程序中任何控制器內的Auth組件。如果你想它只會影響到管理部分,那麼你可以在你的應用程序AppController的Auth初始化中的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')   
     ) 
    ) 
    ) 
} 

,您可以如果您使用的蛋糕2.3.x版本或更高版本,那麼要確保已經指定像正確的格式重定向操作上管理路由綁定這個喜歡

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

return $this->redirect('action_name'); // you can also specify the array of parameters. 
相關問題