1

I'm struggling with Symfony 3.4 and autowiring.

He here an exemple of the error message : Argument 2 passed to App\Controller\MainController::contact() must be an instance of Swift_Mailer, null given

It's the same message with every Service I pass as argument.

There is probably something missing in the configuration but I can't figure out what...

Any clue ?

// config/services.yaml
parameters:
    google_recaptcha_site_key: '%env(GOOGLE_RECAPTCHA_SITE_KEY)%'

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

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

  App\Controller\:
    resource: '../src/Controller'
    public: true
    tags: ['controller.service_arguments']

// src/Controller/MainController.php
namespace App\Controller;

use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Validator\Validation;

class MainController extends AbstractController
{
 public function contact(
        Request $request,
        \Swift_Mailer $mailer,
        Validation $validation,
    ) { ... }
}
Charly
  • 436
  • 4
  • 12
  • 2
    It would be good to add code of your controller's constructor along with `use` statements – Flying Feb 16 '19 at 12:39
  • 1
    Also it is helpful to use Symfony's built-in container debugger: `php -f bin/console debug:container debug:container `, maybe it will give you a hint – Flying Feb 16 '19 at 12:40
  • 1
    Post the configuration of your service and your controller – 113408 Feb 16 '19 at 12:54
  • How do you include `Swift_Mailer` into project? Directly, as `swiftmailer/swiftmailer` or as Symfony bundle `symfony/swiftmailer-bundle`? – Flying Feb 16 '19 at 13:48
  • via symfony/swiftmailer-bundle – Charly Feb 16 '19 at 14:23
  • It looks quite strange then, did you tried to look at `debug:container` command output for `\Swift_Mailer` or `mailer`? – Flying Feb 16 '19 at 14:29
  • debug:container |grep mailer ```Swift_Mailer alias for "swiftmailer.mailer.default" ... ``` – Charly Feb 16 '19 at 15:33
  • 1
    I have no idea to be true, but I would recommend: 1. try to pass mailer to constructor to see if there will be any difference; 2. look inside container itself to see how controller is being instantiated, it is located into `getMainControllerService.php`. Hope it will give some hint to fix the issue – Flying Feb 16 '19 at 18:10
  • Thanks @Flying for your help, I ended creating a new app from scratch and comparing behaviour while adding bundle one by one. CF my answer below. – Charly Feb 17 '19 at 18:25

1 Answers1

0

I did manage to solve my problem.

I had Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle enabled, and it seems that we can't have sensio/framework-extra-bundle and symfony/flex (or maybe with additionnal configuration).

Charly
  • 436
  • 4
  • 12