user1838937
user1838937

Reputation: 299

External service configuration in Symfony 4

I'm trying to use imports in services.yaml to move some services definitions to separate file. Here you can find this in symfony doc.

But when i'm trying to do this I'm getting an error:

Cannot autowire service "App\Domain\Test\Test": argument "$param" of method "__construct()" must have a type-hint or be given a value explicitly.

Here are my config files:

# services.yaml
imports:
    - { resource: services/test.yaml }

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

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

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

And

# test.yaml
parameters:
    param: 'some param'

services:
    App\Domain\Test\Test:
        public: true
        autowire: true
        arguments:
            $param: '%param%'

Here is the service:

namespace App\Domain\Test;

class Test
{
    private $mailer;
    private $param;

    public function __construct(\Swift_Mailer $mailer, string $param)
    {
        $this->mailer = $mailer;
        $this->param = $param;
    }
}

This is working well if I move parameters and service into services.yaml. Do somebody know what I'm doing wrong?

Upvotes: 4

Views: 1323

Answers (1)

Nek
Nek

Reputation: 3105

The problem is that your first file try to load as service your service and can't fill automatically the parameter string $param.

A good fix may be to exclude your service:

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

Another solution is to simply put this definition inside the services.yaml file.

You may also consider to load App\Something\: instead of the complete namespace but this has drawbacks if you try to autowire in cross-namespaces definitions. (some features may not work as expected)

Upvotes: 4

Related Questions