Search code examples
phpsymfonyyamlsymfony5

Move configuration from service.yaml to another file in Symfony5


My config/service.yaml contains several services that have huge configuration. Now, I want to move the configuration of that services to a separate file.

I tried to do like this:

At the end of service.yaml:

imports:
   - { resource: 'packages/custom_service.yaml' }

config/packages/custom_service.yaml:

services:
    App\Service\CustomService:
        arguments:
            $forms:
                - location: 'room1'
                  id: 43543
                - location: 'room2'
                  id: 6476546
                - location: 'room3'
                  id: 121231
        ...

src/Service/CustomService.php:

    /**
     * @var array
     */
    protected $forms;

    public function __construct(array $forms)
    {
        $this->forms = $forms;
    }

But when I try to autowire in some Controller, I am getting this error:

Cannot resolve argument $customService of "App\Controller\CustomController::customAction()": Cannot autowire service "App\Service\CustomService": argument "$forms" of method "__construct()" is type-hinted "array", you should configure its value explicitly.

But if I remove type hint, Then i get this error:

Cannot resolve argument $customService of "App\Controller\CustomController::customAction()": Cannot autowire service "App\Service\CustomService": argument "$forms" of method "__construct()" has no type-hint, you should configure its value explicitly.

Solution

  • The other two answers are partially correct but they don't quite cover everything.

    To start with, anytime you decide you want multiple autowired service files you have to be careful that a given service is only autowired by one and only one file. Otherwise Symfony will attempt to define the service multiple times and usually fail. There are different approaches to accomplishing this but the most straight forward one is to explicitly exclude any custom services your config/services.yaml file:

    # config/services.yaml
    parameters:
    
    imports:
        - { resource: 'services/custom_service.yaml' }
    
    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.
    
        # 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/Service/CustomService.php' # ADD THIS
                - '../src/DependencyInjection/'
                - # the rest of the default excludes
    

    So that takes care of skipping CustomService.php in the main config file.

    Secondly, notice in the above file I loaded the custom_service.yaml file from config/services instead of config/packages. The packages is really reserved for bundle specific configuration. Every file in there is loaded by default. Putting service files in there may or may not work but it definitely changes the loading order of things and could cause problems. So make a directory just for custom services and use it.

    You also need to repeat the _defaults section in each service file since _defaults are considered to be file specific. Bit of a pain perhaps but that is the way it is:

    # config/services/custom_service.yaml
    
    services:
      _defaults:
        autowire: true
        autoconfigure: true
    
      App\Service\CustomService:
        arguments:
          $forms:
            -
                location: 'room1'
                id: 43543
            -
                location: 'room2'
                id: 6476546
            -
                location: 'room3'
                id: 121231
    

    Also note that your yaml array syntax was messed up. Possibly just a copy/paste issue.

    And that should do it.