I'm trying to autowire the PasswordHasherInterface in the Fixtures class:
<?php namespace AppDataFixtures; use AppModelUserEntityUserEmail; use AppModelUserEntityUserId; use AppModelUserEntityUserRole; use AppModelUserEntityUserUser; use DoctrineBundleFixturesBundleFixture; use DoctrinePersistenceObjectManager; use SymfonyComponentPasswordHasherPasswordHasherInterface; class UserFixture extends Fixture { private PasswordHasherInterface $hasher; public function __construct(PasswordHasherInterface $hasher) { $this->hasher = $hasher; } public function load(ObjectManager $manager): void { $hash = $this->hasher->hash("password"); $user = User::signUpByEmail( Id::next(), new DateTimeImmutable(), new Email("admin@app.test"), $hash, "token" ); $user->confirmSignUp(); $user->changeRole(Role::admin()); $manager->persist($user); $manager->flush(); } }
But I got the error:
In DefinitionErrorExceptionPass.php line 54: ! !
!!Unable to autowire service 'AppDataFixturesUserFixture': parameter '$hasher'
!!Method "__construct()" refers to interface "SymfonyComponentPasswordH
!!asherPasswordHasherInterface" but no such service exists. Did you create
!!The class that implements this interface?
! !
My file services.yaml:
parameters: services: # default configuration for services in *this* file _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name App: resource: '../src/' exclude: - '../src/DependencyInjection/' - '../src/Model/User/Entity/' - '../src/Kernel.php'
How to hash plain passwords in Symfony 6.1? Why am I getting this error?
There is no universal
PasswordHasher
.you:
Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface
Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface
for user*.Using a factory, your code would look like this: (untested)
To reiterate, the steps are:
composer require symfony/password-hasher
UserPasswordHasherInterface
orPasswordHasherFactoryInterface
(see Example) and getPasswordHasher
*: The
UserPasswordHasherInterface
example for the fix is located here.