我正在尝试在Fixtures类中自动装配PasswordHasherInterface:
<?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();
}
}
但是我得到了错误:
在 DefinitionErrorExceptionPass.php 第 54 行: !!
!!无法自动装配服务“AppDataFixturesUserFixture”:参数“$hasher”
!!方法“__construct()”引用接口“SymfonyComponentPasswordH
!! asherPasswordHasherInterface”,但不存在此类服务。您是否创建了
!!实现这个接口的类?
!!
我的文件 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'
如何在 Symfony 6.1 中散列纯密码? 为什么我会收到此错误?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
没有通用的
PasswordHasher。您:
Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterfaceSymfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface对于用户*。使用工厂,您的代码将如下所示:(未经测试)
//... 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(); }重申一下,步骤是:
composer require symfony/password-hasherUserPasswordHasherInterface或PasswordHasherFactoryInterface(请参阅示例)并获取PasswordHasher*:修复程序的
UserPasswordHasherInterface示例位于 此处。