【问题标题】:Error: Class 'App\Model\Entity\DefaultPasswordHasher' not found错误:找不到类“App\Model\Entity\DefaultPasswordHasher”
【发布时间】:2015-09-10 15:42:13
【问题描述】:
<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

/**
 * User Entity.
 */
class User extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     * Note that '*' is set to true, which allows all unspecified fields to be
     * mass assigned. For security purposes, it is advised to set '*' to false
     * (or remove), and explicitly make individual fields accessible as needed.
     *
     * @var array
     */
    protected $_accessible = [
        '*' => true,
        'id' => false,
    ];

    protected function _setPassword($value)
    {
        $hasher = new DefaultPasswordHasher();
        return $hasher->hash($value);
    }
}

这是我在 user.php 中的代码。 我正在对密码进行哈希处理并得到了这个错误

错误:找不到类“App\Model\Entity\DefaultPasswordHasher”文件 C:\xamp\htdocs\bookmarker\src\Model\Entity\User.php 行:27

【问题讨论】:

    标签: cakephp-3.0


    【解决方案1】:

    我错过了以下行:

    use Cake\Auth\DefaultPasswordHasher;
    

    这就是我收到错误的原因。

    【讨论】:

    • 我在那条线上收到了Error: syntax error, unexpected '?' 。有什么线索吗?
    • @JackTheKnife 检查你的语法。
    【解决方案2】:

    如果您使用 cakephp version 4.0 和 已经在你的 php 文件中包含了这两个用于密码散列:

    use Authentication\PasswordHasher\DefaultPasswordHasher;
    
    
    {
        $hasher = new DefaultPasswordHasher();
        return $hasher->hash($password);
    }
    

    官方cakephp 4.0 documentation中提到

    仍然会出现这样的错误:

    Undefined type 'Authentication\PasswordHasher\DefaultPasswordHasher'. 
    intelephense(1009) [50,23]
    

    .

    然后代替使用:

     use Authentication\PasswordHasher\DefaultPasswordHasher;
       
     $hasher = new DefaultPasswordHasher();
    

    使用这个:

     use Cake\Auth\DefaultPasswordHasher as AuthDefaultPasswordHasher;
    
     $hasher = new AuthDefaultPasswordHasher();
    

    【讨论】:

      【解决方案3】:

      请使用这个:

      protected function _setPassword($value) {
          return sha1($value);
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-02
      • 2017-12-06
      • 2018-02-25
      • 2020-03-07
      • 1970-01-01
      • 2021-07-20
      • 2020-10-24
      • 2016-11-03
      相关资源
      最近更新 更多