@@ -103,40 +103,7 @@ for you. In order for this to work, you must *teach* the container how to
103
103
create the ``Mailer `` service. This is done via configuration, which can
104
104
be specified in YAML, XML or PHP:
105
105
106
- .. configuration-block ::
107
-
108
- .. code-block :: yaml
109
-
110
- # app/config/config.yml
111
- services :
112
- my_mailer :
113
- class : Acme\HelloBundle\Mailer
114
- arguments : [sendmail]
115
-
116
- .. code-block :: xml
117
-
118
- <!-- app/config/config.xml -->
119
- <?xml version =" 1.0" encoding =" UTF-8" ?>
120
- <container xmlns =" http://symfony.com/schema/dic/services"
121
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
122
- xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
123
-
124
- <services >
125
- <service id =" my_mailer" class =" Acme\HelloBundle\Mailer" >
126
- <argument >sendmail</argument >
127
- </service >
128
- </services >
129
- </container >
130
-
131
- .. code-block :: php
132
-
133
- // app/config/config.php
134
- use Symfony\Component\DependencyInjection\Definition;
135
-
136
- $container->setDefinition('my_mailer', new Definition(
137
- 'Acme\HelloBundle\Mailer',
138
- array('sendmail')
139
- ));
106
+ .. include includes/_service_container_my_mailer.rst.inc
140
107
141
108
.. note ::
142
109
@@ -660,6 +627,113 @@ service needs the ``my_mailer`` service in order to function. When you define
660
627
this dependency in the service container, the container takes care of all
661
628
the work of instantiating the classes.
662
629
630
+ Using the Expression Language
631
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
632
+
633
+ .. versionadded :: 2.4
634
+ The Expression Language functionality was introduced in Symfony 2.4.
635
+
636
+ The service container also supports an "expression" that allows you to inject
637
+ very specific values into a service.
638
+
639
+ For example, suppose you have a third service (not shown here), called ``mailer_configuration ``,
640
+ which has a ``getMailerMethod() `` method on it, which will return a string
641
+ like ``sendmail `` based on some configuration. Remember that the first argument
642
+ to the ``my_mailer `` service is the simple string ``sendmail ``:
643
+
644
+ .. include includes/_service_container_my_mailer.rst.inc
645
+
646
+ But instead of hardcoding this, how could we get this value from the ``getMailerMethod() ``
647
+ of the new ``mailer_configuration `` service? One way is to use an expression:
648
+
649
+ .. configuration-block ::
650
+
651
+ .. code-block :: yaml
652
+
653
+ # app/config/config.yml
654
+ services :
655
+ my_mailer :
656
+ class : Acme\HelloBundle\Mailer
657
+ arguments : ["@=service('mailer_configuration').getMailerMethod()"]
658
+
659
+ .. code-block :: xml
660
+
661
+ <!-- app/config/config.xml -->
662
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
663
+ <container xmlns =" http://symfony.com/schema/dic/services"
664
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
665
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
666
+ http://symfony.com/schema/dic/services/services-1.0.xsd"
667
+ >
668
+
669
+ <services >
670
+ <service id =" my_mailer" class =" Acme\HelloBundle\Mailer" >
671
+ <argument type =" expression" >service('mailer_configuration').getMailerMethod()</argument >
672
+ </service >
673
+ </services >
674
+ </container >
675
+
676
+ .. code-block :: php
677
+
678
+ // app/config/config.php
679
+ use Symfony\Component\DependencyInjection\Definition;
680
+ use Symfony\Component\ExpressionLanguage\Expression;
681
+
682
+ $container->setDefinition('my_mailer', new Definition(
683
+ 'Acme\HelloBundle\Mailer',
684
+ array(new Expression('service("mailer_configuration").getMailerMethod()'))
685
+ ));
686
+
687
+ To learn more about the expression language syntax, see :doc: `/components/expression_language/syntax `.
688
+
689
+ In this context, you have access to 2 functions:
690
+
691
+ * ``service `` - returns a given service (see the example above);
692
+ * ``parameter `` - returns a specific parameter value (syntax is just like ``service ``)
693
+
694
+ You also have access to the :class: `Symfony\\ Component\\ DependencyInjection\\ ContainerBuilder `
695
+ via a ``container `` variable. Here's another example:
696
+
697
+ .. configuration-block ::
698
+
699
+ .. code-block :: yaml
700
+
701
+ services :
702
+ my_mailer :
703
+ class : Acme\HelloBundle\Mailer
704
+ arguments : ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]
705
+
706
+ .. code-block :: xml
707
+
708
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
709
+ <container xmlns =" http://symfony.com/schema/dic/services"
710
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
711
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
712
+ http://symfony.com/schema/dic/services/services-1.0.xsd"
713
+ >
714
+
715
+ <services >
716
+ <service id =" my_mailer" class =" Acme\HelloBundle\Mailer" >
717
+ <argument type =" expression" >@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'</argument >
718
+ </service >
719
+ </services >
720
+ </container >
721
+
722
+ .. code-block :: php
723
+
724
+ use Symfony\Component\DependencyInjection\Definition;
725
+ use Symfony\Component\ExpressionLanguage\Expression;
726
+
727
+ $container->setDefinition('my_mailer', new Definition(
728
+ 'Acme\HelloBundle\Mailer',
729
+ array(new Expression(
730
+ "@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"
731
+ ))
732
+ ));
733
+
734
+ Expressions can be used in ``parameters ``, ``arguments ``, ``properties ``,
735
+ as arguments with ``configurator `` and as arguments to ``calls `` (method calls).
736
+
663
737
Optional Dependencies: Setter Injection
664
738
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
665
739
0 commit comments