Symfony 4.4 + scheb / 2fa:自定义代码生成器和邮件程序

问题描述

我正在尝试遵循本文档,但是yaml配置会引发一些错误。 我对Symfony的了解还不足以了解我在做什么错。 https://github.com/scheb/2fa/blob/5.x/doc/providers/email.md

services.yaml:

removerImg(){
    console.log('Removiendo..');
    let im = document.getElementById('preview').setAttribute('src','../../../../../assets/img/no-image.jpg');      
}

scheb_2fa.yaml:

https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    uploads_path: '/uploads/files/'
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.
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'
    App\Controller\:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']
    # App\Service\CodeGeneratorService:
    #     arguments: ['@markdown.parser','@doctrine_cache.providers.my_markdown_cache']
    auth_mailer:
        # class: Scheb\TwoFactorBundle\Mailer\SymfonyAuthCodeMailer
        class: App\Mailer\MyAuthCodeMailer
    code_generator_service:
        class: App\Service\CodeGeneratorService
        # public: true
        arguments:
            # $mailer: "%scheb_two_factor.security.email.symfony_auth_code_mailer%"
            # $mailer: "%scheb_two_factor.security.email.swift_auth_code_mailer%"
            # $mailer: App\Mailer\MyAuthCodeMailer
            # $mailer: Scheb\TwoFactorBundle\Mailer\SymfonyAuthCodeMailer
            # $mailer: Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface
            # $mailer: App\service\CodeGeneratorService
            # $mailer: "%scheb_two_factor.email.mailer%"
            $digits: "%scheb_two_factor.email.digits%"

App \ Service \ CodeGeneratorService.php:

# See the configuration reference at https://github.com/scheb/2fa/blob/master/doc/configuration.md
scheb_two_factor:
    security_tokens:
        - Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken
    email:
        enabled: true
        sender_email: quezako35@gmail.com
        sender_name: Quezako  # Optional
        digits: 12
        code_generator: code_generator_service  # Use alternative service to generate authentication code
        mailer: auth_mailer  # Use alternative service to send the authentication code

我收到的各种错误消息:

无法自动装配服务“ code_generator_service”:方法“ App \ Service \ CodeGeneratorService :: __ construct()”的参数“ $ mailer”引用接口“ Scheb \ TwoFactorBundle \ Mailer \ AuthCodeMailerInterface”,但不存在此类服务。您也许应该将此接口别名为以下现有服务之一:“ App \ Mailer \ MyAuthCodeMailer”,“ auth_mailer”,“ scheb_two_factor.security.email.swift_auth_code_mailer”,“ scheb_two_factor.security.email.symfony_auth_code_mailer”。

您已请求一个不存在的参数“ scheb_two_factor.email.mailer”。您是说其中之一吗:“ scheb_two_factor.email.sender_name”,“ scheb_two_factor.email.template”,“ scheb_two_factor.email.digits”?

传递给App \ Service \ CodeGeneratorService :: __ construct()的参数2必须实现接口Scheb \ TwoFactorBundle \ Mailer \ AuthCodeMailerInterface,给定的字符串,在D:\ Dev \ meet_the_team \ var \ cache \ dev \ Container0FXY0Rx \ srcApp_KernelDevDebugContainer中调用。 621行上的php

解决方法

作者Christian Scheb给了我答案。

您必须配置服务并使用@批注引用该服务,以告知Symfony使用该服务实例。 https://github.com/scheb/2fa/issues/22

services:
    # ...
    auth_mailer:
        class: App\Mailer\MyAuthCodeMailer

    code_generator_service:
        class: App\Service\CodeGeneratorService
        arguments:
            $mailer: "@auth_mailer"
            $digits: "%scheb_two_factor.email.digits%"

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...