php - 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.

Answer

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.

Answer

Solution:

Autowiring has file scope.

You need to set it in config/packages/custom_service.yaml as well.

Modify that file as follows

services:
  _defaults:
    autowire: true
  
  App\Service\CustomService:
    [...]

You should also add autoconfigure and/or visibility (public keyword) if needed.

Answer

Solution:

Unfortunately, I cannot add a comment so I have to use an answer.

I have to make the following assumption, I believe you have the following snippet inside your services.yaml:

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

which already includes your desired class App\Service\CustomService and so Symfony will try to autowire it before looking inside the imports which leads to the error because you have that class defined inside the import which will only be evaluated if the root services.yaml does not already find the class.

The following example will work. However, you will lose auto-discovery of classes inside the App namespace and it is not recommended to do this.

services.yaml:

parameters:

imports:
    - { resource: 'custom_services.yaml' }

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true
        autoconfigure: true

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

custom_services.yaml:

services:
    _defaults:
        autowire: true
        autoconfigure: true

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

src/Service/SomeService.php:

<?php

declare(strict_types=1);

namespace App\Service;

class SomeService
{
    protected array $forms;

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

    public function getForms()
    {
        return $this->forms;
    }
}

src/Controller/HomeController:

<?php

declare(strict_types=1);

namespace App\Controller;

use App\Service\SomeService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class HomeController
{
    #[Route('/', 'home')]
    public function __invoke(SomeService $someService)
    {
        return new JsonResponse($someService->getForms());
    }
}

What you can do instead is import the file in the Kernel instead in the yaml file which will overwrite the service definition.

So remove the imports part inside your services.yaml and make sure that you import the file inside the src/Kernel.php:

protected function configureContainer(ContainerConfigurator $container): void
{
    $container->import('../config/{packages}/*.yaml');
    $container->import('../config/{packages}/'.$this->environment.'/*.yaml');

    if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
        $container->import('../config/services.yaml');
        
        // Import your custom service files after services.yaml to overwrite already existing definitions
        $container->import('../config/custom_services.yaml');


        $container->import('../config/{services}_'.$this->environment.'.yaml');
    } elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
        (require $path)($container->withPath($path), $this);
    }
}

Source