|
| 1 | +.. index:: |
| 2 | + single: DependencyInjection; Importing Resources |
| 3 | + single: Service Container; Importing Resources |
| 4 | + |
| 5 | +How to Import Configuration Files/Resources |
| 6 | +=========================================== |
| 7 | + |
| 8 | +.. tip:: |
| 9 | + |
| 10 | + In this section, service configuration files are referred to as *resources*. |
| 11 | + This is to highlight the fact that, while most configuration resources |
| 12 | + will be files (e.g. YAML, XML, PHP), Symfony is so flexible that configuration |
| 13 | + could be loaded from anywhere (e.g. a database or even via an external |
| 14 | + web service). |
| 15 | + |
| 16 | +The service container is built using a single configuration resource |
| 17 | +(``app/config/config.yml`` by default). All other service configuration |
| 18 | +(including the core Symfony and third-party bundle configuration) must |
| 19 | +be imported from inside this file in one way or another. This gives you absolute |
| 20 | +flexibility over the services in your application. |
| 21 | + |
| 22 | +External service configuration can be imported in two different ways. The first |
| 23 | +method, commonly used to import other resources, is via the ``imports`` |
| 24 | +directive. The second method, using dependency injection extensions, is used by |
| 25 | +third-party bundles to load the configuration. Read on to learn more about both |
| 26 | +methods. |
| 27 | + |
| 28 | +.. index:: |
| 29 | + single: Service Container; Imports |
| 30 | + |
| 31 | +.. _service-container-imports-directive: |
| 32 | + |
| 33 | +Importing Configuration with ``imports`` |
| 34 | +---------------------------------------- |
| 35 | + |
| 36 | +So far, you've placed your ``app.mailer`` service container definition directly |
| 37 | +in the services configuration file (e.g. ``app/config/services.yml``). If your |
| 38 | +application ends up having many services, this file becomes huge and hard to |
| 39 | +maintain. To avoid this, you can split your service configuration into multiple |
| 40 | +service files: |
| 41 | + |
| 42 | +.. configuration-block:: |
| 43 | + |
| 44 | + .. code-block:: yaml |
| 45 | +
|
| 46 | + # app/config/services/mailer.yml |
| 47 | + parameters: |
| 48 | + app.mailer.transport: sendmail |
| 49 | +
|
| 50 | + services: |
| 51 | + app.mailer: |
| 52 | + class: AppBundle\Mailer |
| 53 | + arguments: ['%app.mailer.transport%'] |
| 54 | +
|
| 55 | + .. code-block:: xml |
| 56 | +
|
| 57 | + <!-- app/config/services/mailer.xml --> |
| 58 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 59 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 60 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 61 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 62 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 63 | +
|
| 64 | + <parameters> |
| 65 | + <parameter key="app.mailer.transport">sendmail</parameter> |
| 66 | + </parameters> |
| 67 | +
|
| 68 | + <services> |
| 69 | + <service id="app.mailer" class="AppBundle\Mailer"> |
| 70 | + <argument>%app.mailer.transport%</argument> |
| 71 | + </service> |
| 72 | + </services> |
| 73 | + </container> |
| 74 | +
|
| 75 | + .. code-block:: php |
| 76 | +
|
| 77 | + // app/config/services/mailer.php |
| 78 | + use Symfony\Component\DependencyInjection\Definition; |
| 79 | +
|
| 80 | + $container->setParameter('app.mailer.transport', 'sendmail'); |
| 81 | +
|
| 82 | + $container->setDefinition('app.mailer', new Definition( |
| 83 | + 'AppBundle\Mailer', |
| 84 | + array('%app.mailer.transport%') |
| 85 | + )); |
| 86 | +
|
| 87 | +The definition itself hasn't changed, only its location. To make the service |
| 88 | +container load the definitions in this resource file, use the ``imports`` key |
| 89 | +in any already loaded resource (e.g. ``app/config/services.yml`` or |
| 90 | +``app/config/config.yml``): |
| 91 | + |
| 92 | +.. configuration-block:: |
| 93 | + |
| 94 | + .. code-block:: yaml |
| 95 | +
|
| 96 | + # app/config/services.yml |
| 97 | + imports: |
| 98 | + - { resource: services/mailer.yml } |
| 99 | +
|
| 100 | + .. code-block:: xml |
| 101 | +
|
| 102 | + <!-- app/config/services.xml --> |
| 103 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 104 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 105 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 106 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 107 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 108 | +
|
| 109 | + <imports> |
| 110 | + <import resource="services/mailer.xml"/> |
| 111 | + </imports> |
| 112 | + </container> |
| 113 | +
|
| 114 | + .. code-block:: php |
| 115 | +
|
| 116 | + // app/config/services.php |
| 117 | + $loader->import('services/mailer.php'); |
| 118 | +
|
| 119 | +The ``resource`` location, for files, is either a relative path from the |
| 120 | +current file or an absolute path. |
| 121 | + |
| 122 | +.. include:: /components/dependency_injection/_imports-parameters-note.rst.inc |
| 123 | + |
| 124 | +.. index:: |
| 125 | + single: Service Container; Extension configuration |
| 126 | + |
| 127 | +.. _service-container-extension-configuration: |
| 128 | + |
| 129 | +Importing Configuration via Container Extensions |
| 130 | +------------------------------------------------ |
| 131 | + |
| 132 | +Third-party bundle container configuration, including Symfony core services, |
| 133 | +are usually loaded using another method that's more flexible and easy to |
| 134 | +configure in your application. |
| 135 | + |
| 136 | +Internally, each bundle defines its services like you've seen so far. However, |
| 137 | +these files aren't imported using the ``import`` directive. These bundles use a |
| 138 | +*dependency injection extension* to load the files. The extension also allows |
| 139 | +bundles to provide configuration to dynamically load some services. |
| 140 | + |
| 141 | +Take the FrameworkBundle - the core Symfony Framework bundle - as an |
| 142 | +example. The presence of the following code in your application configuration |
| 143 | +invokes the service container extension inside the FrameworkBundle: |
| 144 | + |
| 145 | +.. configuration-block:: |
| 146 | + |
| 147 | + .. code-block:: yaml |
| 148 | +
|
| 149 | + # app/config/config.yml |
| 150 | + framework: |
| 151 | + secret: xxxxxxxxxx |
| 152 | + form: true |
| 153 | + # ... |
| 154 | +
|
| 155 | + .. code-block:: xml |
| 156 | +
|
| 157 | + <!-- app/config/config.xml --> |
| 158 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 159 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 160 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 161 | + xmlns:framework="http://symfony.com/schema/dic/symfony" |
| 162 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd |
| 163 | + http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" |
| 164 | + > |
| 165 | +
|
| 166 | + <framework:config secret="xxxxxxxxxx"> |
| 167 | + <framework:form /> |
| 168 | +
|
| 169 | + <!-- ... --> |
| 170 | + </framework> |
| 171 | + </container> |
| 172 | +
|
| 173 | + .. code-block:: php |
| 174 | +
|
| 175 | + // app/config/config.php |
| 176 | + $container->loadFromExtension('framework', array( |
| 177 | + 'secret' => 'xxxxxxxxxx', |
| 178 | + 'form' => array(), |
| 179 | +
|
| 180 | + // ... |
| 181 | + )); |
| 182 | +
|
| 183 | +When the resources are parsed, the container looks for an extension that |
| 184 | +can handle the ``framework`` directive. The extension in question, which lives |
| 185 | +in the FrameworkBundle, is invoked and the service configuration for the |
| 186 | +FrameworkBundle is loaded. |
| 187 | + |
| 188 | +The settings under the ``framework`` directive (e.g. ``form: true``) indicate |
| 189 | +that the extension should load all services related to the Form component. If |
| 190 | +form was disabled, these services wouldn't be loaded and Form integration would |
| 191 | +not be available. |
| 192 | + |
| 193 | +When installing or configuring a bundle, see the bundle's documentation for |
| 194 | +how the services for the bundle should be installed and configured. The options |
| 195 | +available for the core bundles can be found inside the :doc:`Reference Guide </reference/index>`. |
| 196 | + |
| 197 | +.. seealso:: |
| 198 | + |
| 199 | + If you want to use dependency injection extensions in your own shared |
| 200 | + bundles and provide user friendly configuration, take a look at the |
| 201 | + ":doc:`/cookbook/bundles/extension`" cookbook recipe. |
0 commit comments