1

我正在使用 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->request->data['User'] 传递给 $this->Auth->login() 方法并调试了 $this->Session->read(Auth.User.id)。它给了我null。请给我一个解决方案。

4

1 回答 1

1

您可以尝试传递$this->request->data给该$this->Auth->login()方法。这不是一个好方法(参见这篇文章:CakePHP Auth Component Not logging in when using $this->Auth->login();),但它对我有用。你调试过$this->Auth->login()吗?

于 2012-11-02T20:09:54.827 回答