|
1 | 1 | ..index:: |
2 | | - single: Using Configuration Parameters |
3 | | - |
4 | | -Using Configuration Parameters |
5 | | -============================== |
6 | | - |
7 | | -Parameters, such as ``kernel.root_dir`` (pointing to a path) or ``kernel.debug`` |
8 | | -(whether debug mode is enabled), are common in configuration. By wrapping the |
9 | | -parameter name within 2 ``%`` characters, you indicate that this value should |
10 | | -be replaced by the value of the parameter. When using Symfony, you can have |
11 | | -parameters defined in a bundle and used only in that bundle and also parameters |
12 | | -defined in a bundle and used in another. Usually they would have a name |
13 | | -like ``demo_bundle.service_doer.local_parameter`` making explicit where this |
14 | | -parameter comes from. If the parameter is global then it may not have a |
15 | | -namespace, e.g. ``%some_global_option_here%``. |
16 | | - |
17 | | -Basic Usage |
18 | | ------------ |
19 | | - |
20 | | -You can use parameters inside the ``parameters.yml`` file: |
21 | | - |
22 | | -..code-block::yaml |
23 | | -
|
24 | | -# app/config/parameters.yml |
25 | | -parameters: |
26 | | -payment_test_mode:"%kernel.root_dir%" |
27 | | -
|
28 | | -Inside ``config.yml`` and other configuration files building larger |
29 | | -strings: |
30 | | - |
31 | | -..configuration-block:: |
32 | | - |
33 | | - ..code-block::yaml |
34 | | -
|
35 | | -# app/config/config.yml |
36 | | -my_bundle: |
37 | | -local: |
38 | | -directory:"%kernel.root_dir%/../web/media/image" |
39 | | -
|
40 | | - ..code-block::xml |
41 | | -
|
42 | | - <?xml version="1.0" encoding="UTF-8" ?> |
43 | | - <containerxmlns="http://symfony.com/schema/dic/services" |
44 | | -my-bundle="http://example.org/schema/dic/my_bundle"> |
45 | | - <my-bundle:config> |
46 | | - <my-bundle:localdirectory="%kernel.root_dir%/../web/media/image" /> |
47 | | - </my-bundle:config> |
48 | | - </container> |
49 | | -
|
50 | | - ..code-block::php |
51 | | -
|
52 | | - $container->loadFromExtension('my_bundle', array( |
53 | | - 'local' => array( |
54 | | - 'directory' => '%kernel.root_dir%/../web/media/image', |
55 | | - ), |
56 | | - )); |
57 | | -
|
58 | | -For more information on how parameters are used in Symfony please see |
59 | | -:ref:`parameters<book-service-container-parameters>`. |
60 | | - |
61 | | -Besides these usages above you can use this syntax in routing files and handle |
62 | | -parameters in special cases as discussed below. |
63 | | - |
64 | | -Using Parameters in your Bundle Configuration |
65 | | ---------------------------------------------- |
66 | | - |
67 | | -If for instance, there is a use case in which you want to use the |
68 | | -``%kernel.debug%`` debug mode parameter to make your bundle adapt its |
69 | | -configuration depending on this. For this case you cannot use |
70 | | -the syntax directly and expect this to work. The configuration handling |
71 | | -will just treat this ``%kernel.debug%`` as a string. Consider |
72 | | -this example with the AcmeDemoBundle:: |
| 2 | + single: Using Parameters Within A Dependency Injection Class |
| 3 | + |
| 4 | +Using Parameters Within A Dependency Injection Class |
| 5 | +---------------------------------------------------- |
| 6 | + |
| 7 | +You have seen how to use configuration parameters within |
| 8 | +:ref:`Symfony service container<book-service-container-parameters>`. |
| 9 | +There are special cases such as when you want, for instance, to use the |
| 10 | +``%kernel.debug%`` parameter to make the services in your bundle enter |
| 11 | +debug mode. For this case there is more work to do in order |
| 12 | +to make the system understand the parameter value. By default |
| 13 | +your parameter ``%kernel.debug%`` will be treated as a |
| 14 | +simple string. Consider this example with the AcmeDemoBundle:: |
73 | 15 |
|
74 | 16 | // Inside Configuration class |
75 | 17 | $rootNode |
|