لا يعمل autowire الخدمة مع Symfony 4

2

أحاول المرور من Symfony 3.4 إلى Symfony 4.1 ، لكني أواجه مشكلة في autowire. لقد قمت بتثبيت حزمة symfony / swiftmailer ، وفي حالة المشترك لدي:

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;
}

في الخدمة. 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)%'

لكن لدي خطأ:

Cannot autowire service "App\EventListener\ContactNotificationSubscriber": argument "$mailer" of method "__construct()" references class "Swift_Mailer" but no such service exists.

لا أفهم لماذا ... المكون موجود ، باستخدام PhpStorm ، يمكنني النقر فوق \ Swift_Mailer ورؤية الفصل ، لكن Symfony دائمًا ما يعيد إلي خطأ ...

إذا كان شخص يعرف لماذا :-) شكرا جزيلا

1 إجابة

1
افضل جواب

كانت لدي نفس المشكله. بالنسبة لحالتي ، لم يتم تضمين الحزمة في bundles.php . إضافة ما يلي bundles.php حلها لي:

Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
:مؤلف

أسئلة ذات صلة

فوق
قائمة طعام