PasswordHasherInterface kann nicht automatisch verbunden werden
P粉154228483
P粉154228483 2023-12-13 15:55:26
0
1
511

Ich versuche, PasswordHasherInterface in der Fixtures-Klasse automatisch zu verknüpfen:

<?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();
    }
}

Aber ich habe den Fehler erhalten:

In DefinitionErrorExceptionPass.php Zeile 54: ! !

!!Der Dienst „AppDataFixturesUserFixture“ kann nicht automatisch verbunden werden: Parameter „$hasher“

!!Methode „__construct()“ bezieht sich auf die Schnittstelle „SymfonyComponentPasswordH

!!asherPasswordHasherInterface“, aber es existiert kein solcher Dienst. Haben Sie einen erstellt

!!Eine Klasse, die diese Schnittstelle implementiert?

! !

Meine Dateiservices.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'

Wie hashe ich einfache Passwörter in Symfony 6.1? Warum erhalte ich diesen Fehler?

P粉154228483
P粉154228483

Antworte allen(1)
P粉787820396

没有通用的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("admin@app.test"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

        $user->changeRole(Role::admin());

        $manager->persist($user);
        $manager->flush();
    }

重申一下,步骤是:

  1. 安装软件包:composer require symfony/password-hasher
  2. 配置哈希器李>
  3. 加载哈希器
    • 加载UserPasswordHasherInterface
    • 加载PasswordHasherFactoryInterface(请参阅示例)并获取PasswordHasher

*:修复程序的 UserPasswordHasherInterface 示例位于 此处

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage