Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Service Container
  4. How to Import Configuration Files/Resources

How to Import Configuration Files/Resources

Edit this page

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:

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

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

    <parameters>
        <!-- ... some parameters -->
    </parameters>

    <services>
        <!-- ... some services -->
    </services>
</container>
1
2
3
4
// config/services/mailer.php

// ... some parameters
// ... 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# config/services.yaml
imports:
    - { resource: services/mailer.yaml }
    # If you want to import a whole directory:
    - { resource: services/ }
services:
    _defaults:
        autowire: true
        autoconfigure: true

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

    # ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd">

    <imports>
        <import resource="services/mailer.xml"/>
        <!-- If you want to import a whole directory: -->
        <import resource="services/"/>
    </imports>

    <services>
        <defaults autowire="true" autoconfigure="true"/>

        <prototype namespace="App\" resource="../src/*"
            exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}"/>

        <!-- ... -->
    </services>
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return function(ContainerConfigurator $container): void {
    $container->import('services/mailer.php');
    // If you want to import a whole directory:
    $container->import('services/');

    $services = $container->services()
        ->defaults()
            ->autowire()
            ->autoconfigure()
    ;

    $services->load('App\\', '../src/*')
        ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
};

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 does not work:

1
2
3
# config/services.yaml
imports:
    - { resource: '%kernel.project_dir%/somefile.yaml' }
1
2
3
4
5
6
7
8
9
10
11
<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd"
>
    <imports>
        <import resource="%kernel.project_dir%/somefile.yaml"/>
    </imports>
</container>
1
2
3
4
5
6
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $container): void {
    $container->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.
TOC
    Version

    Symfony 7.1 is backed by

    Become certified from home

    Become certified from home

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Version:

    Table of Contents

    • Importing Configuration with imports
    • Importing Configuration via Container Extensions

    Symfony footer

    Avatar of Guillaume Aveline, a Symfony contributor

    Thanks Guillaume Aveline for being a Symfony contributor

    2 commits • 9 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony