Rumah > Soal Jawab > teks badan
Saya sedang cuba untuk mengaitkan antaramuka PasswordHasher dalam kelas Jadual:
<?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(); } }
Tetapi saya mendapat ralat:
Dalam DefinitionErrorExceptionPass.php baris 54: ! !
!!Tidak dapat autowire perkhidmatan 'AppDataFixturesUserFixture': parameter '$hasher'
!!Kaedah "__construct()" merujuk kepada antara muka "SymfonyComponentPasswordH
!!asherPasswordHasherInterface" tetapi tiada perkhidmatan sedemikian wujud. Adakah anda mencipta satu
!!Kelas yang melaksanakan antara muka ini?
! !
Perkhidmatan fail saya.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'
Bagaimana untuk mencincang kata laluan biasa dalam Symfony 6.1? Mengapa saya mendapat ralat ini?
P粉7878203962023-12-14 09:25:15
Tiada satu saiz yang sesuai untuk semua PasswordHasher
.
Anda:
SymfonyComponentPasswordHasherHasherPasswordHasherFactoryInterface
SymfonyComponentPasswordHasherHasherUserPasswordHasherInterface
untuk pengguna*. Menggunakan kilang, kod anda akan kelihatan seperti ini: (belum diuji)
//... use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface; class UserFixture extends Fixture { private PasswordHasherFactoryInterface $passwordHasherFactory; public function __construct(PasswordHasherFactoryInterface $hasherFactory) { $this->passwordHasherFactory = $passwordHasherFactory; } public function load(ObjectManager $manager): void { $passwordHasher = $this->passwordHasherFactory->getPasswordHasher(User::class); $hash = $passwordHasher->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(); }
Untuk mengulangi, langkah-langkahnya ialah:
composer require symfony/password-hasher
UserPasswordHasherInterface
atau PasswordHasherFactoryInterface
(lihat contoh PasswordHasherFactoryInterface
(请参阅示例)并获取PasswordHasher
) dan dapatkan
UserPasswordHasherInterface
*: Contoh pembetulan terletak di sini