当我登录时:
$this->Auth->login($this->request->data)
现在,当我说:
$this->set('current_user', $this->Auth->user());
在我的 $current_user 中只有用户名和密码。在我的数据库中没有 ID、first_name、last_name 等。为什么?我需要那个数据!
我试过 $this->Auth->login() - 没有 $this->request->data 但这没有用。
当我登录时:
$this->Auth->login($this->request->data)
现在,当我说:
$this->set('current_user', $this->Auth->user());
在我的 $current_user 中只有用户名和密码。在我的数据库中没有 ID、first_name、last_name 等。为什么?我需要那个数据!
我试过 $this->Auth->login() - 没有 $this->request->data 但这没有用。
您不需要将请求数据发送到登录函数,如果匹配,它将根据当前的帖子数据返回一个用户。
这是我的 UserController.php 中的登录功能
public function login() {
if($this->Auth->user()) {
$this->redirect('/');
}
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password.'));
}
}
}
要在视图中查找我的用户字段,我在 AppController.php 中进行了如下设置:
class AppController extends Controller {
public function beforeFilter() {
$this->set('user', $this->Auth->user());
}
}
然后在$user您的所有视图中都可以使用。
index.ctp
if($user) {
echo var_dump($user);
}
array
'id' => string '15' (length=2)
'created' => string '2012-06-29 21:50:44' (length=19)
'modified' => string '2012-06-29 21:50:44' (length=19)
'group_id' => string '1' (length=1)
'username' => string 'user' (length=4)
'email' => string 'email@email.com' (length=15)
echo $user['id']; -> 15
echo $user['email']; -> email@email.com
该Auth::login()方法在 2.0 中的工作方式发生了变化。您现在不需要将 post 数据传递给 login 方法,它会自动从请求对象中获取数据。
有关更多信息,请参见此处的红框:http: //book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in