Cakephp 3.1,为什么$this->;认证->;识别();总是返回false?
UsersController.php的登录操作=>Cakephp 3.1,为什么$this->;认证->;识别();总是返回false?,php,cakephp,login,cakephp-3.0,Php,Cakephp,Login,Cakephp 3.0,UsersController.php的登录操作=> public function login() { if ($this->request->is('post')) { $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user); return $this->
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('Username or password is incorrect'), [
'key' => 'auth'
]);
}
}
}
public function add()
{
$this->set('groups', $this->Users->Groups->find('list'));
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$article = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($article)) {
$this->Flash->success(__('New user has been saved.'));
return $this->redirect(['controller' => 'posts', 'action' => 'index']);
}
$this->Flash->error(__('Unable to add new user.'));
}
}
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Posts',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Posts',
'action' => 'index'
]
]);
$this->Auth->allow();
}
我已经创建了一个用户。
添加UsersController.php的操作=>
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('Username or password is incorrect'), [
'key' => 'auth'
]);
}
}
}
public function add()
{
$this->set('groups', $this->Users->Groups->find('list'));
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$article = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($article)) {
$this->Flash->success(__('New user has been saved.'));
return $this->redirect(['controller' => 'posts', 'action' => 'index']);
}
$this->Flash->error(__('Unable to add new user.'));
}
}
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Posts',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Posts',
'action' => 'index'
]
]);
$this->Auth->allow();
}
用户实体=>
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User extends Entity
{
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
}
login.ctp=>
<h2>Login</h2>
<?php
echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' =>'login')));
echo $this->Form->input('User.username',array('style'=>'width:50%'));
echo $this->Form->input('User.password',array('style'=>'width:50%'));
echo $this->Form->submit('Login');
echo $this->Form->end();
?>
以以下格式保存在数据库用户表中的密码:$2y$10$ZP9WngYH74tdlOfBKpix2ORfvs/NN2.WIstWpg7qgGpoSwDhaMU8q
为什么我不能登录?为什么$this->Auth->identify();总是返回false
我们将非常感谢您的帮助
提前感谢。您确实需要在身份验证配置中添加要检查的字段,如:
'Form' => [
'fields' => [
'username' => 'username',
'password' => 'password'
]
]
此外,您的表单是错误的,它需要像下面这样读取,从控制器传递一个实体
$this->Form->create($userEntity);
不要在字段前面加上(错误的)模型,只需使用:
$this->Form->input('username');
$this->Form->input('password');
请包括您的AuthComponent配置。是的,请确保您的AppController AuthComponent设置正确。您确实错过了AppController中的AuthComponent配置,并且至少错过了login.ctp。@Davidell,我已经编辑了提供auth component配置的原始问题。@hmic,我已经编辑了提供login.ctp的原始问题。您可以发布完整的身份验证配置吗?$user的价值是多少?它将在哪里定义?这是本书中完整的示例:
$user
是从控制器设置为视图的实体。对于将是$user=$this->Users->newEntity()
的add()
操作,并且将是对edit()
操作的查询。