[英]cakephp - $this->Auth->user() sometimes returns $user['User']['user']['id'] and other times $user['user']['id']
使用時
$user = $this->Auth->user();
要獲取有關當前登錄用戶的詳細信息,有時返回的數組如下:
> user
> User
> Office
Message
還有其他時候:
> user
id
name
> Office
這很煩人,因為在我的視圖中我輸出了用戶名,有時候因為額外的['用戶'而在數組中是一個額外的級別,有時它不是。
如何控制或了解我如何知道期望的版本?
謝謝
編輯這里是我的登錄操作
public function login() {
$this->layout = 'loginlayout';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
// did they select the remember me checkbox? - http://stackoverflow.com/questions/12447487/cakephp-remember-me-with-auth
if ($this->request->data['User']['remember_me'] == 1) {
// remove "remember me checkbox"
unset($this->request->data['User']['remember_me']);
// hash the user's password
$this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);
// write the cookie
$this->Cookie->write('remember_me_cookie', $this->request->data['User'], true, '4 weeks');
}
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(('Username or password is incorrect'));
}
}
}
另外在我的app控制器的過濾器之前,我有:
if (!$this->Auth->loggedIn() && $this->Cookie->read('remember_me_cookie')) {
$cookie = $this->Cookie->read('remember_me_cookie');
$user = $this->User->find('first', array(
'conditions' => array(
'User.email' => $cookie['email'],
'User.password' => $cookie['password']
)
));
if ($user && !$this->Auth->login($user)) {
$this->redirect('/users/logout'); // destroy session & cookie
}
}
是否有可能通過cookie登錄導致這種不同的結果?
所以
$this->Auth->login($user)
如果您希望Auth會話數據始終位於該結構中,因為它來自login()本身,這是一個壞主意。 而且你已經知道它增加了一個額外的“用戶”鍵。 所以你需要做的就是調試()$ user數據,你會發現它需要它
$this->Auth->login($user['User'])
PS:順便說一句,它也記錄在食譜中。 當你查看源代碼時也在代碼中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.