我正在尝试在Fixtures类中 Autowiring PasswordHasherInterface:
<?php
namespace App\DataFixtures;
use App\Model\User\Entity\User\Email;
use App\Model\User\Entity\User\Id;
use App\Model\User\Entity\User\Role;
use App\Model\User\Entity\User\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cede8e1e5e2ccedfcfca2f8e9fff8" rel="noreferrer noopener nofollow">[email protected]</a>"),
$hash,
"token"
);
$user->confirmSignUp();
$user->changeRole(Role::admin());
$manager->persist($user);
$manager->flush();
}
}
但是我得到了错误:
在 DefinitionErrorExceptionPass.php 第 54 行:
!!
!!无法 Autowiring 服务“App\DataFixtures\UserFixture”:参数“$hasher”
!!方法“__construct()”引用接口(interface)“Symfony\Component\PasswordH
!! asher\PasswordHasherInterface”,但不存在此类服务。您是否创建了
!!实现这个接口(interface)的类?
!!
我的文件 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 中散列纯密码? 为什么我会收到此错误?
最佳答案
没有通用的PasswordHasher
。
您:
- 使用工厂生成一个特定的:例如
Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface
- 或者您使用专用的密码哈希器类:例如
Symfony\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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0c1c4cdc9cee0c1d0d08ed4c5d3d4" rel="noreferrer noopener nofollow">[email protected]</a>"),
$hash,
"token"
);
$user->confirmSignUp();
$user->changeRole(Role::admin());
$manager->persist($user);
$manager->flush();
}
重申一下,步骤是:
- 安装软件包:
composer require symfony/password-hasher
- Configure the hasher
- 加载哈希器
- 加载
UserPasswordHasherInterface
或 - 加载
PasswordHasherFactoryInterface
(请参阅example)并获取PasswordHasher
- 加载
*:固定装置的 UserPasswordHasherInterface
示例位于 here .
关于php - 无法 Autowiring PasswordHasherInterface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72874895/