CakePHP 3.3 - 验证不工作
CakePHP 3.3 - Auth not working
我是 CakePHP 的新手,我看过 tutorial on YouTube 关于如何实现登录视图的内容。
我是按照教程一步一步来的,但是$user
好像永远都不是真的。即使我尝试使用正确的凭据登录,我也会收到 "Incorrect Login" 错误。
我的错误在哪里?
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);
}
找到办法了,我把$this->$password
传给了hash函数,应该是$password
而已
我是 CakePHP 的新手,我看过 tutorial on YouTube 关于如何实现登录视图的内容。
我是按照教程一步一步来的,但是$user
好像永远都不是真的。即使我尝试使用正确的凭据登录,我也会收到 "Incorrect Login" 错误。
我的错误在哪里?
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);
}
找到办法了,我把$this->$password
传给了hash函数,应该是$password
而已