Cakephp为什么$user在尝试登录时为false

Cakephp为什么$user在尝试登录时为false,cakephp,cakephp-3.x,Cakephp,Cakephp 3.x,我试图用CakePHP3.8创建一个登录和注册页面。我已经成功地创建了注册页面,所有的数据都输入到数据库中,但是一旦我尝试登录,它就不会每次都起作用。我看不出有任何错误。每次我输入电子邮件和密码时,它都会显示“无效的电子邮件或密码,请重试” 代码如下: UserController.php <?php namespace App\Controller; use Cake\Controller\Controller; use Cake\Event\Event; class UsersCo

我试图用CakePHP3.8创建一个登录和注册页面。我已经成功地创建了注册页面,所有的数据都输入到数据库中,但是一旦我尝试登录,它就不会每次都起作用。我看不出有任何错误。每次我输入电子邮件和密码时,它都会显示“无效的电子邮件或密码,请重试”

代码如下:

UserController.php

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;

class UsersController extends AppController
{
 public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->Auth->allow('add');
    $this->Auth->allow(['add', 'logout']);
}

public function register()
{
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        // Prior to 3.4.0 $this->request->data() was used.
        $user = $this->Users->patchEntity($user, $this->request->getData());
        if ($this->Users->save($user)) {
            $this->Flash->success(__('The user has been saved.'));
            return $this->redirect(['action' => 'register']);
        }
        $this->Flash->_error_(__('Unable to add the user.'));
    }
    $this->set('user', $user);
}



public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->_error_(__('Invalid email or password, try again'));
    }
}

public function logout()
{
    return $this->redirect($this->Auth->logout());
}
} 
public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Users',
            'action' => 'register'
        ],
        'logoutRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home'
        ]
    ]);
}

    public function beforeFilter(Event $event)
{
    $this->Auth->allow(['index', 'view', 'display','register']);
} 
 public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Users',
            'action' => 'register'
        ],
        'logoutRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home'
        ],
        'authenticate'=>[
            'Form'=>[
                'fields'=>['email'=>'email','password'=>'password']
            ]
        ]
    ]);
}

    public function beforeFilter(Event $event)
{
    $this->Auth->allow(['index', 'view', 'display','register','add']);
    $this->set('email',$this->Auth->user('email'));
}
User.php

 protected function _setPassword($password)
   {
      if (strlen($password) > 0) {
        return (new DefaultPasswordHasher)->hash($password);
    }
}
AppCotroller.php

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;

class UsersController extends AppController
{
 public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->Auth->allow('add');
    $this->Auth->allow(['add', 'logout']);
}

public function register()
{
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        // Prior to 3.4.0 $this->request->data() was used.
        $user = $this->Users->patchEntity($user, $this->request->getData());
        if ($this->Users->save($user)) {
            $this->Flash->success(__('The user has been saved.'));
            return $this->redirect(['action' => 'register']);
        }
        $this->Flash->_error_(__('Unable to add the user.'));
    }
    $this->set('user', $user);
}



public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->_error_(__('Invalid email or password, try again'));
    }
}

public function logout()
{
    return $this->redirect($this->Auth->logout());
}
} 
public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Users',
            'action' => 'register'
        ],
        'logoutRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home'
        ]
    ]);
}

    public function beforeFilter(Event $event)
{
    $this->Auth->allow(['index', 'view', 'display','register']);
} 
 public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Users',
            'action' => 'register'
        ],
        'logoutRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home'
        ],
        'authenticate'=>[
            'Form'=>[
                'fields'=>['email'=>'email','password'=>'password']
            ]
        ]
    ]);
}

    public function beforeFilter(Event $event)
{
    $this->Auth->allow(['index', 'view', 'display','register','add']);
    $this->set('email',$this->Auth->user('email'));
}
login.ctp

<div class="users form">
<?= $this->Flash->render() ?>
<?= $this->Form->create() ?>
<fieldset>
   <legend><?= __('Please enter your email and password') ?></legend>
   <?= $this->Form->control('email') ?>
   <?= $this->Form->control('password') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>


您的字段配置需要告诉它使用哪个字段作为用户名:

'fields'=>['username'=>'email','password'=>'password']

在AppController初始化函数()中尝试此操作

并且请允许登录功能(在您的用户控制器中)


请将代码包括在您的位置,特别是您告诉它使用
电子邮件
而不是
用户名
作为标识字段的位置。@GregSchmidt我添加了它,感谢使用此->验证->允许('add')创建新用户,然后再次检查。@AlimonKarim我需要把它放在哪里?很可能您的哈希密码没有完全保存,因为数据库中的密码字段设置不正确。请尝试VARCHAR 60,并保存新密码。@Alastair您能告诉我一些错误吗?您可以转储()或pr()用于尝试登录的实体或数据。