Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Class 'App\Model\Entity\DefaultPasswordHasher' not found

Tags:

cakephp-3.0

<?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);
    }
}

Here is my code in user.php. I am hashing the password and got this one error

Error: Class 'App\Model\Entity\DefaultPasswordHasher' not found File C:\xamp\htdocs\bookmarker\src\Model\Entity\User.php Line: 27

like image 809
Asif Mehmood Avatar asked Sep 10 '15 15:09

Asif Mehmood


2 Answers

If you using cakephp version 4.0 and already included these two for password hashing in your php file:

use Authentication\PasswordHasher\DefaultPasswordHasher;


{
    $hasher = new DefaultPasswordHasher();
    return $hasher->hash($password);
}

as mention in official cakephp 4.0 documentation

And still get an error like this:

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

.

Then instead of using:

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

use this:

 use Cake\Auth\DefaultPasswordHasher as AuthDefaultPasswordHasher;

 $hasher = new AuthDefaultPasswordHasher();
like image 75
Anurag Dabas Avatar answered Nov 05 '22 14:11

Anurag Dabas


I was missing the following line:

use Cake\Auth\DefaultPasswordHasher;

This was the reason why I got the error.

like image 24
Asif Mehmood Avatar answered Nov 05 '22 12:11

Asif Mehmood