@@ -630,17 +630,20 @@ the work of instantiating the classes.
630
630
Using the Expression Language
631
631
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
632
632
633
+ .. versionadded :: 2.4
634
+ The Expression Language functionality was introduced in Symfony 2.4.
635
+
633
636
The service container also supports an "expression" that allows you to inject
634
637
very specific values into a service.
635
638
636
639
For example, suppose you have a third service (not shown here), called ``mailer_configuration ``,
637
- which has a ``getMailerMethod `` method on it, which will return a string
640
+ which has a ``getMailerMethod() `` method on it, which will return a string
638
641
like ``sendmail `` based on some configuration. Remember that the first argument
639
642
to the ``my_mailer `` service is the simple string ``sendmail ``:
640
643
641
644
.. include includes/_service_container_my_mailer.rst.inc
642
645
643
- But instead of hardcoding this, how could we get this value from the ``getMailerMethod ``
646
+ But instead of hardcoding this, how could we get this value from the ``getMailerMethod() ``
644
647
of the new ``mailer_configuration `` service? One way is to use an expression:
645
648
646
649
.. configuration-block ::
@@ -659,7 +662,9 @@ of the new ``mailer_configuration`` service? One way is to use an expression:
659
662
<?xml version =" 1.0" encoding =" UTF-8" ?>
660
663
<container xmlns =" http://symfony.com/schema/dic/services"
661
664
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
662
- xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
665
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
666
+ http://symfony.com/schema/dic/services/services-1.0.xsd"
667
+ >
663
668
664
669
<services >
665
670
<service id =" my_mailer" class =" Acme\HelloBundle\Mailer" >
@@ -672,10 +677,11 @@ of the new ``mailer_configuration`` service? One way is to use an expression:
672
677
673
678
// app/config/config.php
674
679
use Symfony\Component\DependencyInjection\Definition;
680
+ use Symfony\Component\ExpressionLanguage\Expression;
675
681
676
682
$container->setDefinition('my_mailer', new Definition(
677
683
'Acme\HelloBundle\Mailer',
678
- array(new Expression(" service(' mailer_configuration' ).getMailerMethod()" ))
684
+ array(new Expression(' service(" mailer_configuration" ).getMailerMethod()' ))
679
685
));
680
686
681
687
To learn more about the expression language syntax, see :doc: `/components/expression_language/syntax `.
@@ -694,15 +700,17 @@ via a ``container`` variable. Here's another example:
694
700
695
701
services :
696
702
my_mailer :
697
- class : Acme\HelloBundle\Mailer
698
- arguments : ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]
703
+ class : Acme\HelloBundle\Mailer
704
+ arguments : ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]
699
705
700
706
.. code-block :: xml
701
707
702
708
<?xml version =" 1.0" encoding =" UTF-8" ?>
703
709
<container xmlns =" http://symfony.com/schema/dic/services"
704
710
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
705
- xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
711
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
712
+ http://symfony.com/schema/dic/services/services-1.0.xsd"
713
+ >
706
714
707
715
<services >
708
716
<service id =" my_mailer" class =" Acme\HelloBundle\Mailer" >
@@ -714,10 +722,13 @@ via a ``container`` variable. Here's another example:
714
722
.. code-block :: php
715
723
716
724
use Symfony\Component\DependencyInjection\Definition;
725
+ use Symfony\Component\ExpressionLanguage\Expression;
717
726
718
727
$container->setDefinition('my_mailer', new Definition(
719
728
'Acme\HelloBundle\Mailer',
720
- array(new Expression("@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"))
729
+ array(new Expression(
730
+ "@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"
731
+ ))
721
732
));
722
733
723
734
Expressions can be used in ``parameters ``, ``arguments ``, ``properties ``,
0 commit comments