CakePHP 2.0:登录表单不起作用

时间:2012-03-03 11:14:37

标签: cakephp-2.0

我正在尝试创建一个新的注册和登录页面。我在登录时遇到问题。

1)注册用户名和密码后,它成功散列并保存到数据库中,请找到以下代码:(一切按照蛋糕惯例)

user.php的:

<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
......
public function beforeSave() {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}
}
?>

UsersController.php:

public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->saveAll($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array('controller' => 'Posts','action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

public function login() {
    if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash(__('Invalid username or password, try again'), 'default', array(), 'auth');
    }
}

public function logout() {
    $this->redirect($this->Auth->logout());
}

login.ctp:

<fieldset>
<?php echo $this->Session->flash('auth'); ?>
</fieldset>
<?php echo $this->Form->create('Users');?>
<fieldset>
<?php
    echo $this->Form->input('username');
    echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login'));?>

调试:(调试($ this-&gt; data);)

来自AppController:

Array
(
[Users] => Array
    (
        [username] => vinodronold
        [password] => vinodronold
    )

)

来自UsersController:

Array
(
[Users] => Array
    (
        [username] => vinodronold
        [password] => vinodronold
    )

)

问题:

每当我访问/ Users / login URL时,我都会收到“用户名或密码无效,请再试一次”消息。

虽然我输入了正确的用户名和密码,但我无法登录。

请帮忙。

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

仔细观察,将登录功能的if语句更改为:

if (!empty($this->request->data['User']) && $this->Auth->login()) {

这将修复访问该页面时收到的即时错误消息。

我只是注意到你的视图中的表单是错误的。模型是单数的。将表单创建更改为:

<?php echo $this->Form->create('User');?>

这应该适用于这两个问题。