Symfony configuration
Configuration files
Symfony applications are configured using the files stored in the config/
directory which has the following default structure:
your-project/
├─ config/
│ ├─ packages/
│ ├─ bundles.php
│ ├─ routes.yaml
│ └─ services.yaml
- The
config/packages
directory stores the configuration of every package installed in your application; - The
bundles.php
file enables/disables packages in your application; - The
routes.yaml
file defines the routing configuration; - The
services.yaml
file configures the services of the service container.
When using Symfony Flex (enabled by default in Symfony applications), Symfony packages will automatically update the config/bundles.php
file and create a new file in config/packages/
to contains its configuration.
For example, the API Platform package creates this file:
# config/packages/api_platform.yaml
api_platform:
mapping:
paths: ['%kernel.project_dir%/src/Entity']
Configuration parameters
If you have configuration values that need to be reused in several configuration files, you can define it as a “parameter”. You can think of a configuration parameter as a reusable configuration value.
Define a parameter
By convention, parameters are defined under the parameters
key in the config/services.yaml
file. For example:
# config/services.yaml
parameters:
# the parameter name is an arbitrary string (the 'app.' prefix is recommended
# to better differentiate your parameters from Symfony parameters).
app.admin_email: 'something@example.com'
# boolean parameters
app.enable_v2_protocol: true
# array/collection parameters
app.supported_locales: ['en', 'es', 'fr']
# binary content parameters (encode the contents with base64_encode())
app.some_parameter: !!binary VGhpcyBpcyBhIEJlbGwgY2hhciAH
# PHP constants as parameter values
app.some_constant: !php/const GLOBAL_CONSTANT
app.another_constant: !php/const App\Entity\BlogPost::MAX_ITEMS
# Enum case as parameter values
app.some_enum: !php/enum App\Enum\PostState::Published
# ...
Use a parameter in another configuration file
You can wrap the parameter name in two %
symbols to reference it from other configuration files:
# config/packages/some_package.yaml
some_package:
# any string surrounded by two % is replaced by that parameter value
email_address: '%app.admin_email%'
Note that due to the way parameters are resolved, you cannot concatenate parameters in the following way:
# config/services.yaml
imports:
- { resource: '%kernel.project_dir%/somefile.yaml' }
List all config parameters
bin/console debug:container --parameters
Use a parameter in a service or controller
See here.