0

I have a class in App\Util which needs the MailerInterface Dependency. So I added it directly to the constructor like this:

public function __construct(MailerInterface $mailer)
    {
        $this->mailer = $mailer;
    }

Then I added the argument in the services.yaml:

services: 
        ...
        App\Util\OwnerMailValidation:
              arguments: ['@mailer']

In the end I used exactly the same code as provided by the symfony documentation but I keep getting the error: Too few arguments to function App\Util\OwnerMailValidation::__construct(), 0 passed [...] 1 expected

My complete servicey.yaml:

services:
    _defaults:
        autowire: true      
        autoconfigure: true

    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'

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

    App\Util\OwnerMailValidation:
        class: App\Util\OwnerMailValidation
        arguments: ['@mailer']

7
  • Which version of symfony are you using? Could you provide the autowiring part of your services.yaml file please. Something like App\: resource: '../src/*' exclude: '../src/{Entity,Migrations,Tests}' Commented Feb 2, 2021 at 13:21
  • I am using Symfony v4.22.0. I added the service.yaml to the thread (but I did not change anything there yet (besided the new OwnerMailValidation etry) @Mcsky Commented Feb 2, 2021 at 13:24
  • 1
    You probably don't need the service configuration lines in your services.yaml file at all. Autowire will take care of it. I suspect that you are new'ing your OwnerMailValidation class instead of injecting it. The PHP new operator knows nothing about the Symfony DI container. Commented Feb 2, 2021 at 13:29
  • @Cerad that's what I thought it would do, but it gives me the same error again. Commented Feb 2, 2021 at 14:17
  • 3
    And like I said in my original comment, the new operator has no knowledge of the Symfony Dependency Injection container. Using new will simply will not work. This is actually a fairly common misconception. Go back to the basics and read the docs on the service container. Maybe create a new test project and add a few services following the examples until you understand what is going on. Commented Feb 2, 2021 at 15:56

2 Answers 2

1

If you have a single implementation of MailerInterface you shouldn't write anything in your service.yaml
Symfony can figure it out on his own.
If it still doesn't work try to do this

App\Util\OwnerMailValidation:
    arguments:
      $mailer: '@<the class that implements MailerInteface that you want to inject>'
      

As @cerad said autowiring works if you inject your class into another.
If you're doing $validation = new OwnerMailValidation(); it just won't work because it requires a MailerInterface in the constructor.
You should inject your OwnerMailValidation into the controller method or into the class constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Just for info, if it was an argument related problem then the error message would be significantly different.
0

Try this config:

services: 
        ...
        App\Util\OwnerMailValidation:
           class: App\Util\OwnerMailValidation

2 Comments

Thanks for the answer. I added the class but it does not change anything. I keep getting the same error
@nikserg - Just for info, if class is omitted then the service id is assumed to be the fully qualified class name. It is all in the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.