|
| 1 | +.. index:: |
| 2 | + single: Configuration, Environments |
| 3 | + |
| 4 | +How to Organize Configuration Files |
| 5 | +=================================== |
| 6 | + |
| 7 | +The default Symfony2 Standard Edition defines three |
| 8 | +:doc:`execution environments </cookbook/configuration/environments>` called |
| 9 | +``dev``, ``prod``, and ``test``. An environment simply represents a way to |
| 10 | +execute the same codebase with different configuration. |
| 11 | + |
| 12 | +In order to select the configuration file to load for each environment, Symfony |
| 13 | +executes the ``registerContainerConfiguration()`` method of the ``AppKernel`` |
| 14 | +class:: |
| 15 | + |
| 16 | + // app/AppKernel.php |
| 17 | + class AppKernel extends Kernel |
| 18 | + { |
| 19 | + // ... |
| 20 | + |
| 21 | + public function registerContainerConfiguration(LoaderInterface $loader) |
| 22 | + { |
| 23 | + $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml'); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | +This method loads the ``app/config/config_dev.yml`` file for the ``dev`` |
| 28 | +environment and so on. In turn, this file loads the common configuration file |
| 29 | +located at ``app/config/config.yml``. Therefore, the configuration files of the |
| 30 | +default Symfony Standard Edition follow this structure: |
| 31 | + |
| 32 | +.. code-block:: text |
| 33 | +
|
| 34 | + <your-project>/ |
| 35 | + ├─ app/ |
| 36 | + │ └─ config/ |
| 37 | + │ ├─ config.yml |
| 38 | + │ ├─ config_dev.yml |
| 39 | + │ ├─ config_prod.yml |
| 40 | + │ ├─ config_test.yml |
| 41 | + │ ├─ parameters.yml |
| 42 | + │ ├─ parameters.yml.dist |
| 43 | + │ ├─ routing.yml |
| 44 | + │ ├─ routing_dev.yml |
| 45 | + │ └─ security.yml |
| 46 | + ├─ src/ |
| 47 | + ├─ vendor/ |
| 48 | + └─ web/ |
| 49 | +
|
| 50 | +This default structure was choosen for its simplicity — one file per environment. |
| 51 | +But as any other Symfony feature, you can customize it to better suit your needs. |
| 52 | +The following sections explain different ways to organize your configuration |
| 53 | +files. In order to simplify the examples, only the ``dev`` and ``prod`` |
| 54 | +environments are taken into account. |
| 55 | + |
| 56 | +Different Directories per Environment |
| 57 | +------------------------------------- |
| 58 | + |
| 59 | +Instead of suffixing the files with ``_dev`` and ``_prod``, this technique |
| 60 | +groups all the related configuration files under a directory with the same |
| 61 | +name as the environment: |
| 62 | + |
| 63 | +.. code-block:: text |
| 64 | +
|
| 65 | + <your-project>/ |
| 66 | + ├─ app/ |
| 67 | + │ └─ config/ |
| 68 | + │ ├─ common/ |
| 69 | + │ │ ├─ config.yml |
| 70 | + │ │ ├─ parameters.yml |
| 71 | + │ │ ├─ routing.yml |
| 72 | + │ │ └─ security.yml |
| 73 | + │ ├─ dev/ |
| 74 | + │ │ ├─ config.yml |
| 75 | + │ │ ├─ parameters.yml |
| 76 | + │ │ ├─ routing.yml |
| 77 | + │ │ └─ security.yml |
| 78 | + │ └─ prod/ |
| 79 | + │ ├─ config.yml |
| 80 | + │ ├─ parameters.yml |
| 81 | + │ ├─ routing.yml |
| 82 | + │ └─ security.yml |
| 83 | + ├─ src/ |
| 84 | + ├─ vendor/ |
| 85 | + └─ web/ |
| 86 | +
|
| 87 | +To make it work, change the code of the ``registerContainerConfiguration()`` |
| 88 | +method:: |
| 89 | + |
| 90 | + // app/AppKernel.php |
| 91 | + class AppKernel extends Kernel |
| 92 | + { |
| 93 | + // ... |
| 94 | + |
| 95 | + public function registerContainerConfiguration(LoaderInterface $loader) |
| 96 | + { |
| 97 | + $loader->load(__DIR__.'/config/'.$this->getEnvironment().'/config.yml'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +Then, make sure that each ``config.yml`` file loads the rest of the configuration |
| 102 | +files, including the common files: |
| 103 | + |
| 104 | +.. code-block:: yaml |
| 105 | +
|
| 106 | + # app/config/dev/config.yml |
| 107 | + imports: |
| 108 | + - { resource: '../config.yml' } |
| 109 | + - { resource: 'parameters.yml' } |
| 110 | + - { resource: 'security.yml' } |
| 111 | +
|
| 112 | + # ... |
| 113 | +
|
| 114 | + # app/config/prod/config.yml |
| 115 | + imports: |
| 116 | + - { resource: '../config.yml' } |
| 117 | + - { resource: 'parameters.yml' } |
| 118 | + - { resource: 'security.yml' } |
| 119 | +
|
| 120 | + # ... |
| 121 | +
|
| 122 | +
|
| 123 | + # app/config/common/config.yml |
| 124 | + imports: |
| 125 | + - { resource: 'parameters.yml' } |
| 126 | + - { resource: 'security.yml' } |
| 127 | +
|
| 128 | + # ... |
| 129 | +
|
| 130 | +
|
| 131 | +Semantic Configuration Files |
| 132 | +---------------------------- |
| 133 | + |
| 134 | +A different organization strategy may be needed for complex applications with |
| 135 | +large configuration files. You could for instance create one file per bundle |
| 136 | +and several files to define all the application services: |
| 137 | + |
| 138 | +.. code-block:: text |
| 139 | +
|
| 140 | + <your-project>/ |
| 141 | + ├─ app/ |
| 142 | + │ └─ config/ |
| 143 | + │ ├─ bundles/ |
| 144 | + │ │ ├─ bundle1.yml |
| 145 | + │ │ ├─ bundle2.yml |
| 146 | + │ │ ├─ ... |
| 147 | + │ │ └─ bundleN.yml |
| 148 | + │ ├─ environments/ |
| 149 | + │ │ ├─ common.yml |
| 150 | + │ │ ├─ dev.yml |
| 151 | + │ │ └─ prod.yml |
| 152 | + │ ├─ routing/ |
| 153 | + │ │ ├─ common.yml |
| 154 | + │ │ ├─ dev.yml |
| 155 | + │ │ └─ prod.yml |
| 156 | + │ └─ services/ |
| 157 | + │ ├─ frontend.yml |
| 158 | + │ ├─ backend.yml |
| 159 | + │ ├─ ... |
| 160 | + │ └─ security.yml |
| 161 | + ├─ src/ |
| 162 | + ├─ vendor/ |
| 163 | + └─ web/ |
| 164 | +
|
| 165 | +Again, change the code of the ``registerContainerConfiguration()`` method to |
| 166 | +make Symfony aware of the new file organization:: |
| 167 | + |
| 168 | + // app/AppKernel.php |
| 169 | + class AppKernel extends Kernel |
| 170 | + { |
| 171 | + // ... |
| 172 | + |
| 173 | + public function registerContainerConfiguration(LoaderInterface $loader) |
| 174 | + { |
| 175 | + $loader->load(__DIR__.'/config/environments/'.$this->getEnvironment().'.yml'); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | +Advanced Tecniques |
| 180 | +------------------ |
| 181 | + |
| 182 | +Symfony loads configuration files using the ``Config component </components/config>``, |
| 183 | +which provides some advanced features. |
| 184 | + |
| 185 | +Mix and Match Configuration Formats |
| 186 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 187 | + |
| 188 | +Configuration files can import files defined with any other built-in configuration |
| 189 | +format (``.yml``, ``.xml``, ``.php``, ``.ini``): |
| 190 | + |
| 191 | +.. code-block:: yaml |
| 192 | +
|
| 193 | + # app/config/config.yml |
| 194 | + imports: |
| 195 | + - { resource: 'parameters.yml' } |
| 196 | + - { resource: 'services.xml' } |
| 197 | + - { resource: 'security.yml' } |
| 198 | + - { resource: 'legacy.php' } |
| 199 | +
|
| 200 | + # ... |
| 201 | +
|
| 202 | +If you use any other configuration format, you have to define your own loader |
| 203 | +class extending it from ``Symfony\Component\DependencyInjection\Loader\FileLoader``. |
| 204 | +When the configuration values are dynamic, you can use the PHP configuration |
| 205 | +file to execute your own logic. In addition, you can define your own services |
| 206 | +to load configuration from databases and web services. |
| 207 | + |
| 208 | +Directory Loading |
| 209 | +~~~~~~~~~~~~~~~~~ |
| 210 | + |
| 211 | +Splitting configuration into lots of smaller files can rapidly become cumbersome |
| 212 | +when importing those files from the main configuration file. Avoid these problems |
| 213 | +by loading an entire directory: |
| 214 | + |
| 215 | +.. code-block:: yaml |
| 216 | +
|
| 217 | + # app/config/config.yml |
| 218 | + imports: |
| 219 | + - { resource: 'bundles/' } |
| 220 | + - { resource: 'services/' } |
| 221 | +
|
| 222 | + # ... |
| 223 | +
|
| 224 | +The Config component will look for recursively in the ``bundles/`` and ``services/`` |
| 225 | +directories and it will load any supported file format (``.yml``, ``.xml``, |
| 226 | +``.php``, ``.ini``). |
| 227 | + |
| 228 | +Global Configuration Files |
| 229 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 230 | + |
| 231 | +Some system administrators may prefer to store sensitive parameteres in global |
| 232 | +configuration files under the ``/etc`` directory. Imagine that the database |
| 233 | +credentials for your website are stored in the ``/etc/sites/mysite.com/parameters.yml``. |
| 234 | +Loading this file is as simple as indicating the full file path when importing |
| 235 | +it from any other configuration file: |
| 236 | + |
| 237 | +.. code-block:: yaml |
| 238 | +
|
| 239 | + # app/config/config.yml |
| 240 | + imports: |
| 241 | + - { resource: 'parameters.yml' } |
| 242 | + - { resource: '/etc/sites/mysite.com/parameters.yml' } |
| 243 | +
|
| 244 | + # ... |
| 245 | +
|
| 246 | +Most of the time, local developers won't have the same files that exist in the |
| 247 | +production servers. For that reason, the Config component provides the |
| 248 | +``ignore_errors`` option to silently discard errors when the loaded file |
| 249 | +doesn't exist: |
| 250 | + |
| 251 | +.. code-block:: yaml |
| 252 | +
|
| 253 | + # app/config/config.yml |
| 254 | + imports: |
| 255 | + - { resource: 'parameters.yml' } |
| 256 | + - { resource: '/etc/sites/mysite.com/parameters.yml', ignore_errors: true } |
| 257 | +
|
| 258 | + # ... |
0 commit comments