我是 CakePHP 的新手,我在 YouTube上观看了有关如何实现登录视图的教程。
我一步一步地遵循教程,但$user
似乎从来都不是真的。即使我尝试使用正确的凭据登录,我也会收到“登录错误”错误。
我的错误在哪里?
UsersController.php 中的登录功能
public function login() {
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
/* never gets here ****************/
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'posts']);
}
//Bad Login
$this->Flash->error('Incorrect Login');
}
}
在 AppController 中初始化 Auth
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
]
]);
登录表单
<br>
<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
<div class="panel">
<h2 class="text-center">
LOGIN
</h2>
<?= $this->Form->create(); ?>
<?= $this->Form->input('email'); ?>
<?= $this->Form->input('password', array('type' => 'password')); ?>
<?= $this->Form->submit('Login', array('class' => 'button')); ?>
<?= $this->Form->end(); ?>
</div>
</div>
用户模型中的 _setPassword
protected function _setPassword($password) {
return (new DefaultPasswordHasher)->hash($this->$password);
}