0
我試圖爲我的web項目實施基於ACL的授權。 我有一個登錄方法:CakePHP 2.0驗證 - >用戶()
function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
debug($this->Auth->user());
//return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
而不是重定向的,我打印登錄的用戶信息。這效果很好。 不過,如果我debug($this->Auth->user())
在AppController
function beforeFilter() {
parent::beforeFilter();
debug($this->Auth->user());
$user = $this->Auth->user();
if ($user) {
$this->set('isAuthed', true);
$this->_userId = (int) $user['User']['id'];
} else {
$this->set('isAuthed', false);
}
}
我什麼也沒得到。可能是什麼問題?
你是否在'AppController'中包含'Auth'組件? – Ross 2012-01-17 13:41:20
@Ross是的,當然。看起來'Session' helper工作不正常。如果我用'$ this-> Session-> write('test',test)'在'UsersController'中寫入了某些內容,我無法在'AppController'中讀取它。有任何想法嗎? – nil 2012-01-17 17:40:41
您可能已經檢查過,但在您的瀏覽器中啓用了Cookie?如果是這樣,每次請求後CAKEPHP cookie的值是否更新? (順便說一下,在AppController中,你不需要調用'parent :: beforeFilter()',因爲它是'Controller'中的一個空方法) – nIcO 2012-01-18 22:53:52