Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate a string, a parameter and a function return result in Symfony Expression Language

Tags:

php

symfony

I'm using Symfony 2.4 and it's Expression Language component. In one of my service definitions, I want to use an argument with type="expression". How is it possible to concatenate the following four components using expression language:

  1. service('kernel').getCacheDir() A function result
  2. "/" Just a slash
  3. %router.cache_class_prefix% A parameter
  4. %corebundle.route_provider.cache.class% A parameter

I hope I was clear in what I want to achieve. Below you see a part of the services.xml file where I want to use it. But this throws an error: SyntaxError: Unexpected token "string" of value "/" around position 33.

<parameters>
    <parameter key="corebundle.route_provider.cache.class">ZikulaRoutes</parameter>
</parameters>
<services>
    <service id="corebundle.route_provider" class="%corebundle.route_provider.class%">
        <call method="setCachePath">
            <argument type="expression">service('kernel').getCacheDir() "/" %router.cache_class_prefix% %corebundle.route_provider.cache.class%</argument>
        </call>
    </service>
like image 593
Christian Avatar asked Nov 02 '25 08:11

Christian


1 Answers

The concatenation operator in the Expression Language is the tilde char ~. The correct syntax for this expression is:

service('kernel').getCacheDir() ~ '/' ~ parameter('router.cache_class_prefix') ~ parameter('corebundle.route_provider.cache.class')
like image 126
Sukei Avatar answered Nov 03 '25 23:11

Sukei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!