mpiot
mpiot

Reputation: 1522

Service autowire don't work with Symfony 4

I try to pass from Symfony 3.4 to Symfony 4.1, but I've a problem with autowire. I've the symfony/swiftmailer-bundle installed, and in an event subscriber I have:

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

In the 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)%'

But I've an error:

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

I don't understand why... The component exists, with PhpStorm, I can click on \Swift_Mailer and see the class, but Symfony always return to me an error...

If someone know why :-) Thanks a lot

Upvotes: 3

Views: 5378

Answers (2)

NajibBoun
NajibBoun

Reputation: 11

You can change

    App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

to :

    App\:
resource: '../src/*'
exclude: '../src/{Entity,EventListener,Migrations,Tests,Kernel.php}'

Upvotes: 0

Nicodemuz
Nicodemuz

Reputation: 4114

I was having the same problem. For my case, the bundle was not being included in bundles.php. Adding the following in bundles.php solved it for me:

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

Upvotes: 3

Related Questions