@@ -35,23 +35,48 @@ inheritance. For more information, see :doc:`/cookbook/bundles/inheritance`.
35
35
Services & Configuration
36
36
------------------------
37
37
38
- In order to override/extend a service, there are two options. Firstly , you can
38
+ In order to override/extend a service, there are two options. First , you can
39
39
set the parameter holding the service's class name to your own class by setting
40
- it in the config.yml. This of course is only possible if the class name is
40
+ it in `` app/ config/config .yml`` . This of course is only possible if the class name is
41
41
defined as a parameter in the service config of the bundle containing the
42
- service. Secondly, if this is not the case, or if you want to make sure the
43
- class is always overridden when your bundle is used, you should use a compiler
44
- pass:
42
+ service. For example, to override the class used for Symfony's ``translator ``
43
+ service, you would override the ``translator.class `` parameter. Knowing exactly
44
+ which parameter to override may take some research. For the translator, the
45
+ parameter is defined and used in the ``Resources/config/translation.xml `` file
46
+ in the core FrameworkBundle:
47
+
48
+ .. configuration-block ::
49
+
50
+ .. code-block :: yaml
51
+
52
+ # app/config/config.yml
53
+ parameters :
54
+ translator.class : Acme\HelloBundle\Translation\Translator
55
+
56
+ .. code-block :: xml
57
+
58
+ <!-- app/config/config.xml -->
59
+ <parameters >
60
+ <parameter key =" translator.class" >Acme\HelloBundle\Translation\Translator</parameter >
61
+ </parameters >
62
+
63
+ .. code-block :: php
64
+
65
+ // app/config/config.php
66
+
67
+ $container->setParameter('translator.class', 'Acme\HelloBundle\Translation\Translator');
68
+
69
+ Secondly, if the class is not available as a parameter, you want to make sure the
70
+ class is always overridden when your bundle is used, or you need to modify
71
+ something beyond just the class name, you should use a compiler pass::
45
72
46
- .. code-block :: php
47
73
namespace Foo\BarBundle\DependencyInjection\Compiler;
48
74
49
75
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
50
76
use Symfony\Component\DependencyInjection\ContainerBuilder;
51
77
52
78
class OverrideServiceCompilerPass implements CompilerPassInterface
53
79
{
54
-
55
80
public function process(ContainerBuilder $container)
56
81
{
57
82
$definition = $container->getDefinition('original-service-id');
@@ -60,11 +85,11 @@ pass:
60
85
}
61
86
62
87
In this example we fetch the service definition of the original service, and set
63
- it's class name to our own class.
88
+ its class name to our own class.
64
89
65
- See `/cookbook/service_container/compiler_passes ` for information on how to use
90
+ See :doc: `/cookbook/service_container/compiler_passes ` for information on how to use
66
91
compiler passes. If you want to do something beyond just overriding the class -
67
- like adding a method call - You can only use the compiler pass method.
92
+ like adding a method call - you can only use the compiler pass method.
68
93
69
94
Entities & Entity mapping
70
95
-------------------------
0 commit comments