我正在使用cakephp 2.1,我在UsersController中使用了登录操作,如下所示。
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid email or password, try again', 'default/flash_error');
}
}
}
login.ctp代码如下。
<?php echo $this->Form->create('User', array('class' => 'form')); ?>
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<?php echo $this -> Form -> text('email', array('id' => 'inputEmail', 'placeholder' => 'Email')); ?>
<?php echo $this -> Form -> error('email', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<?php echo $this -> Form -> password('password', array('id' => 'inputPassword', 'placeholder' => 'Password')); ?>
<?php echo $this -> Form -> error('password', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"/>
Remember me </label>
<?php echo $this->Form->button('Sign in', array('type' => 'submit', 'class' => 'btn')); ?>
</div>
</div>
<?php echo $this->Form->end(); ?>
当表单通过电子邮件和密码提交时,用户无法登录,因此显示错误“无效的电子邮件或密码,请重试”。即使我将$ this-&gt; request-&gt; data ['User']传递给$ this-&gt; Auth-&gt; login()方法并调试$ this-&gt; Session-&gt; read(Auth.User) 。ID)。它让我无效。请给我一个解决方案。
答案 0 :(得分:1)
您可以尝试将$this->request->data
传递到$this->Auth->login()
方法。这不是一个好方法(参见这篇文章:CakePHP Auth Component Not logging in when using $this->Auth->login();),但它对我有用。你调试了$this->Auth->login()
吗?