无法自动passwordhasherinterface
我正在尝试在“固定装置”类中自动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("[email protected]"),
$hash,
"token"
);
$user->confirmSignUp();
$user->changeRole(Role::admin());
$manager->persist($user);
$manager->flush();
}
}
但是我有错误:
在DefacitionerRorexceptionpass.php行54: !!
!!无法自动服务“应用程序\ datafixtures \ userFixture”:参数“ $ hasher”
!!方法“ __construct()”参考接口“ Symfony \ Component \ passwordh
!! ASHER \ passwordHasherInterface”但是没有这样的服务。您是否创建了
!!实现此接口的类?
!!
我的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'
如何在Symfony 6.1中使用平原密码? 为什么我会遇到这个错误?
I'm trying to autowire PasswordHasherInterface in the Fixtures class:
<?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("[email protected]"),
$hash,
"token"
);
$user->confirmSignUp();
$user->changeRole(Role::admin());
$manager->persist($user);
$manager->flush();
}
}
But I got error:
In DefinitionErrorExceptionPass.php line 54:
!!
!! Cannot autowire service "App\DataFixtures\UserFixture": argument "$hasher"
!! of method "__construct()" references interface "Symfony\Component\PasswordH
!! asher\PasswordHasherInterface" but no such service exists. Did you create a
!! 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 password in Symfony 6.1?
Why I get this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有一般
passwordhasher
。您要么:
symfony \ component \ compents \ passwordHasher \ hasher \ passwordHasherFactoryInterface
symfony \ component \ component \ component \ passwordHasher \ passwordHasher \ passwordHasher \ hasher \ hasher \ hasher \ userpasswordhasherinterface
用于用户*。使用工厂,您的代码看起来像这样:(未经测试)
只是为了重申,这些步骤是:
作曲家要求Symfony/password-Hasher
userpasswordhasherinterface
或passwordHasherFactoryInterface
(请参阅示例)并获取passwordhasher
*:
userpasswordhasherinterface
的示例位于在这里。There is no general
PasswordHasher
.Either you:
Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface
Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface
for users*.Using a factory, your code would look like this: (untested)
Just to reiterate, the steps are:
composer require symfony/password-hasher
UserPasswordHasherInterface
ORPasswordHasherFactoryInterface
(see example) and get thePasswordHasher
*: An example of
UserPasswordHasherInterface
for a fixure is located here.