首页 文章

服务autowire不适用于Symfony 4

提问于
浏览
2

我尝试从Symfony 3.4传递到Symfony 4.1,但我遇到了autowire的问题 . 我安装了symfony / swiftmailer-bundle,在事件订阅者中我有:

public function __construct(\Swift_Mailer $mailer, EngineInterface $templating, EntityManagerInterface $em, $senderMail, $senderName)
{
    $this->mailer = $mailer;
    $this->templating = $templating;
    $this->em = $em;
    $this->senderMail = $senderMail;
    $this->senderName = $senderName;
}

在service.yaml中:

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: 'en'

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.
    public: false       # Allows optimizing the container by removing unused services; this also means
                        # fetching services directly from the container via $container->get() won't work.
                        # The best practice is to be explicit about your dependencies anyway.

    # 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/{Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
    resource: '../src/Controller'
    tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones


# Twig
    twig.extension.text:
       class: Twig_Extensions_Extension_Text
       tags:
       - { name: twig.extension }

# Listeners
    App\EventListener\ContactNotificationSubscriber:
    $senderMail: '%env(MAILER_SENDER_ADDRESS)%'
    $senderName: '%env(MAILER_SENDER_NAME)%'

但我有一个错误:

无法自动装配服务“App \ EventListener \ ContactNotificationSubscriber”:方法“__construct()”的参数“$ mailer”引用类“Swift_Mailer”但不存在此类服务 .

我不明白为什么......该组件存在,使用PhpStorm,我可以点击\ Swift_Mailer并查看该类,但Symfony总是给我一个错误...

如果有人知道原因:-)非常感谢

1 回答

  • 1

    我遇到了同样的问题 . 对于我的情况,捆绑包没有包含在 bundles.php 中 . 在 bundles.php 中添加以下内容为我解决了:

    Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
    

相关问题