我是CakePhp框架的新手,并关注了博客教程。一切顺利,直到进行身份验证为止。
以下两行是问题所在:
<?= $this->Form->input('Usuario') ?>
<?= $this->Form->input('Contraseña') ?>
正确的行是:
<?= $this->Form->input('username') ?>
<?= $this->Form->input('password') ?>
这是我从UsersControllers.php登录的方法,调试行始终返回false。
public function login()
{
if ($this->request->is('post')) {
debug($this->Auth->identify());
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Usuario o contraseña inválida, intente nuevamente'));
}
}
这是我的用户类,我不得不更改受保护的行$ _accessible = ['*'=> true]; 如教程中所示,否则将不会保存用户。
class User extends Entity
{
// Make all fields mass assignable for now.
protected $_accessible = ['username' => true,'password'=>true,'role'=>true,'created'=>true,'modified'=>true];
// ...
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
// ...
}
这是我的login.ctp
<div class="users form">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Ingrese su usuario y contraseña') ?></legend>
<?= $this->Form->input('Usuario') ?>
<?= $this->Form->input('Contraseña') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
这些是我的AppController.php方法:
public function initialize()
{
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Expedientes',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'display',
'home'
]
]);
}
public function beforeFilter(Event $event)
{
$this->Auth->allow(['index', 'view']);
}
此时,我可以添加用户,其密码已散列,但无法登录。任何帮助将不胜感激。提前致谢。
感谢@ndm的答案。我在以下几行中翻译了用户名和密码字段:
<?= $this->Form->input('Usuario') ?>
<?= $this->Form->input('Contraseña') ?>
正确的行是:
<?= $this->Form->input('username') ?>
<?= $this->Form->input('password ') ?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句