How to Import Configuration Files/Resources

How to Import Configuration Files/Resources

Tip

In this section, service configuration files are referred to as resources. While most configuration resources are files (e.g. YAML, XML, PHP), Symfony is able to load configuration from anywhere (e.g. a database or even via an external web service).

The service container is built using a single configuration resource (config/services.yaml by default). This gives you absolute flexibility over the services in your application.

External service configuration can be imported in two different ways. The first method, commonly used to import other resources, is via the imports directive. The second method, using dependency injection extensions, is used by third-party bundles to load the configuration. Read on to learn more about both methods.

Importing Configuration with imports

By default, service configuration lives in config/services.yaml. But if that file becomes large, you’re free to organize into multiple files. Suppose you decided to move some configuration to a new file:

  • YAML

    1. # config/services/mailer.yaml
    2. parameters:
    3. # ... some parameters
    4. services:
    5. # ... some services
  • XML

    1. <!-- config/services/mailer.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <parameters>
    8. <!-- ... some parameters -->
    9. </parameters>
    10. <services>
    11. <!-- ... some services -->
    12. </services>
    13. </container>
  • PHP

    1. // config/services/mailer.php
    2. // ... some parameters
    3. // ... some services

To import this file, use the imports key from any other file and pass either a relative or absolute path to the imported file:

  • YAML

    1. # config/services.yaml
    2. imports:
    3. - { resource: services/mailer.yaml }
    4. # If you want to import a whole directory:
    5. - { resource: services/ }
    6. services:
    7. _defaults:
    8. autowire: true
    9. autoconfigure: true
    10. App\:
    11. resource: '../src/*'
    12. exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
    13. # ...
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <imports>
    8. <import resource="services/mailer.xml"/>
    9. <!-- If you want to import a whole directory: -->
    10. <import resource="services/"/>
    11. </imports>
    12. <services>
    13. <defaults autowire="true" autoconfigure="true"/>
    14. <prototype namespace="App\" resource="../src/*"
    15. exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}"/>
    16. <!-- ... -->
    17. </services>
    18. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. return function(ContainerConfigurator $configurator) {
    4. $configurator->import('services/mailer.php');
    5. // If you want to import a whole directory:
    6. $configurator->import('services/');
    7. $services = $configurator->services()
    8. ->defaults()
    9. ->autowire()
    10. ->autoconfigure()
    11. ;
    12. $services->load('App\\', '../src/*')
    13. ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
    14. };

When loading a configuration file, Symfony loads first the imported files and then it processes the parameters and services defined in the file. If you use the default services.yaml configuration as in the above example, the App\ definition creates services for classes found in ../src/*. If your imported file defines services for those classes too, they will be overridden.

A possible solution for this is to add the classes and/or directories of the imported files in the exclude option of the App\ definition. Another solution is to not use imports and add the service definitions in the same file, but after the App\ definition to override it.

Note

Due to the way in which parameters are resolved, you cannot use them to build paths in imports dynamically. This means that something like the following doesn’t work:

  • YAML

    1. # config/services.yaml
    2. imports:
    3. - { resource: '%kernel.project_dir%/somefile.yaml' }
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <imports>
    8. <import resource="%kernel.project_dir%/somefile.yaml"/>
    9. </imports>
    10. </container>
  • PHP

    1. // config/services.php
    2. $loader->import('%kernel.project_dir%/somefile.yaml');

Importing Configuration via Container Extensions

Third-party bundle container configuration, including Symfony core services, are usually loaded using another method: a container extension.

Internally, each bundle defines its services in files like you’ve seen so far. However, these files aren’t imported using the import directive. Instead, bundles use a dependency injection extension to load the files automatically. As soon as you enable a bundle, its extension is called, which is able to load service configuration files.

In fact, each configuration file in config/packages/ is passed to the extension of its related bundle - e.g. FrameworkBundle or TwigBundle - and used to configure those services further.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.