CakePHP无法登录身份验证

CakePHP无法登录身份验证,php,cakephp,authentication,login,Php,Cakephp,Authentication,Login,我是CakePHP新手,我尝试按照CakePHP文档中的示例身份验证和登录,但仍然无法登录 我在其他人身上也看到了同样的问题,但我试着像他们那样去解决,但对我来说没有效果。即使提供了正确的凭据,我也无法登录,但是$this->Auth->login()返回false 这是我的密码 <?php //UsersController public function add() { if ($this->request->is('post')) {

我是CakePHP新手,我尝试按照CakePHP文档中的示例身份验证和登录,但仍然无法登录

我在其他人身上也看到了同样的问题,但我试着像他们那样去解决,但对我来说没有效果。即使提供了正确的凭据,我也无法登录,但是
$this->Auth->login()
返回false

这是我的密码

<?php
//UsersController
public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
    }

public function beforeFilter() {
            parent::beforeFilter();
            // Allow users to register and logout.
            $this->Auth->allow('add', 'logout');
            parent::beforeFilter();
        }


public function login() {
            if ($this->request->is('post')) {
                debug($this->Auth->login());
                if ($this->Auth->login()) {
                    return $this->redirect($this->Auth->redirect());
                }
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }

//User Model
public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'author')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );


    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $passwordHasher = new BlowfishPasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }
        return true;
    }


// Login View

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend>
            <?php echo __('Please enter your username and password'); ?>
        </legend>
        <?php echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

?>

?>
更改此功能:

public function beforeFilter() {
    parent::beforeFilter();
    // Allow users to register and logout.
    $this->Auth->allow('add', 'logout', 'login'); //Add login
}

像那样更新AppController

  public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish'
            )
        )
    )
);

似乎您没有声明Auth组件。您只需参考我的示例代码就可以做到这一点

public $components = array('Auth' => array('authenticate' => array(
            'Form' => array(
                'userModel' => **YOUR MODEL** 
            )
        ),
        'loginAction' => array(
            'controller' => **YOUR CONTROLLER**, 
            'action' => 'login'   
                ),
        'loginRedirect' => array(
            'controller' => **YOUR CONTROLLER**,
            'action' => 'index'
        ),
        'logoutRedirect' => array(
            'controller' => **YOUR CONTROLLER**,
            'action' => 'login'
        )
    )
  );
您可能想先阅读文档或跟随他们的博客教程,因为这在我刚开始时对我有很大帮助。:)


您不允许用户登录,但允许用户注销。而您应该允许登录,但不允许注销