1

I created a parameter in my parameters file:

parameters:
    category:
        var: test

I can access this in PHP by fetching it like this:

$var = $this->container->getParameter('category');
$var = $var['var'];

But how can I access this parameter in my config.yml? For example, I want to pass this parameter to all my twig files as a global twig variable:

twig:
    globals:
        my_var: %category.var% # throws ParameterNotFoundException

(Sidequestion:
I assumed I could access it via getParamter('category.var'), but got an error there. Do you know a nicer way than my two-liner? $this->container->getParameter('category')['var'] works, but is a syntax error according to my IDE.)

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
  • have you tried category_var ? – Udan Oct 24 '13 at 10:54
  • Yeah, I have tried that. Not working either. – Gottlieb Notschnabel Oct 24 '13 at 10:56
  • 1
    here you can find answer http://stackoverflow.com/questions/19839157/how-to-access-nested-parameter-values-in-symfony2 – palmic Mar 16 '14 at 14:36
  • 1
    If you're using NetBeans just change `Project Properties -> Sources -> PHP Version` to higher, because function array dereferencing has been added in PHP 5.4 (and yes, NetBeans is smart enough to check it and tell it's "syntax error"). – Wirone Jul 17 '14 at 13:24

1 Answers1

1

$this->container->getParameter('category')['var']

..is actually a pretty good way to go. Which version of PHP is your IDE using for its syntax checking? Somebody please correct me, but I think this behavior was made valid in 5.3 or 5.4.

lewsid
  • 1,900
  • 2
  • 17
  • 19
  • As for your second question, perhaps this will help you - http://stackoverflow.com/questions/6787895/how-to-get-config-parameters-in-symfony2-twig-templates – lewsid Oct 24 '13 at 10:54
  • I am using [Aptana Studio 3](http://www.aptana.org/). Thanks for your answer on the sidequestion, but I am more interested in the answer for the main question. The linked question does not help me as there are no nested parameters (but parameters with `.` inside...) – Gottlieb Notschnabel Oct 24 '13 at 10:55