CakePHP:获取loggedin用户数据

CakePHP:获取loggedin用户数据,cakephp,authentication,userinfo,Cakephp,Authentication,Userinfo,我所做的 我使用了食谱的这一部分来创建我的身份验证:链接 完成后,我将AppController.php中的字段更改为email和password: public function beforeFilter() { $this->Auth->allow('index'); $this->Auth->fields = array('username' => 'email', 'password' => 'password'); } 发生了什么事

我所做的

我使用了食谱的这一部分来创建我的身份验证:链接 完成后,我将AppController.php中的字段更改为email和password:

public function beforeFilter() {
    $this->Auth->allow('index');
    $this->Auth->fields = array('username' => 'email', 'password' => 'password');
}
发生了什么事

登录表单总是说我的密码/电子邮件组合不正确。 在搜索和尝试了一段时间之后,我将$this->request->数据添加到UsersController的登录函数中的Auth->login参数中:

public function login(){
    $this->layout = 'login';
    if ($this->request->is('post')){
        if ($this->Auth->login($this->request->data)){
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Ongeldig email adres of wachtwoord. Probeer het AUB opnieuw'));
        }
    }
}
这是有效的,但是现在我不能使用$user['id']来获取loggedin用户id。它说它不知道$user变量

我预料会发生什么

首先,它应该在没有我添加参数的情况下让用户登录。其次,它应该打印loggedin用户的id

我希望有人能帮我解决这个问题。
提前谢谢

字段现在位于authenticate数组中:

   'authenticate' => array(
        'Form' => array(
            'fields' => array('username' => 'email', ...)
        )
    )

或者

$this->Auth->authenticate = ...

但是,正如PHP自由撰稿人所说,这是有很好的文档记录的


你用了什么版本的烹饪书?2.x one?

$this->Auth->login$this->request->data=>这是您可能在这里做的最糟糕的事情。马上把它拿开。这与1=1相同-对于经过身份验证的登录,这是毫无意义的!您使用的是哪个CakePHP版本?从2.2开始,你必须按照@mark在回答中说的去做。我使用CakePHP2.2.3。但是我不得不添加$this->request->data,因为Auth->login会返回false,这是不应该的。明天我会再试一次,谢谢你的帮助;我将$components更改为:public$components=array'Session'、'Auth'=>array'authenticate'=>array'Form'=>array'userModel'=>User'、'fields'=>array'username'=>email'、'password'=>password'、'loginDirect'=>array'controller'=>twitter','操作'=>'索引','logoutRedirect'=>数组'控制器'=>'用户','操作'=>'登录','授权'=>数组'控制器';但是现在它只是转到登录重定向,而帐户不存在。
public $components = array(
    'Auth' => array(
        'authenticate' => ...
    )
);