0

Say I have a app/config/parameters.yml which contains

parameters:
    database_driver:   pdo_mysql
    ...
nacho_image: 
    upload: true
    progress: false 
    key: secret_id

Now, I would like to get upload, progress, key in my controller, but doing

$this->get('upload'); 
$this->get('nacho_image.upload'); 
$this->get('nacho.image.upload'); 
$this->getParameter('nacho.upload'); 
$this->getParameter('acho_image.upload.upload'); 

doesn't work...

ʎɹnɔɹǝW
  • 811
  • 2
  • 8
  • 14
  • 1
    Possible duplicate of [How do I read configuration settings from Symfony2 config.yml?](http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml) – KhorneHoly Jan 19 '17 at 13:32
  • @KhorneHoly This is not the same, I am not nesting the configs under `parameters` – ʎɹnɔɹǝW Jan 19 '17 at 13:35
  • It is, have a look at the second answer, that's what you're looking for. – KhorneHoly Jan 19 '17 at 13:37

2 Answers2

3

You cannot directly get a nested parameter. you have to get the parent key and you will get all the content as an array

$nachoImageConfig = $this->getParameter('nacho_image');
$nachoImageUpload = $nachoImageConfig['upload'];

From the doc (http://symfony.com/doc/current/service_container/parameters.html):

The used . notation is just a Symfony convention to make parameters easier to read. Parameters are just flat key-value elements, they can't be organized into a nested array

That is why in most config files, the entries under 'parameters:' are fully qualified like this:

parameters:

    ...

    nacho_image.upload: true
    nacho_image.progress: false 
    nacho_image.key: secret_id
ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
  • I get an error `nacho_image must be defined` same as I was getting before, do you think there is a problem with nesting unrelated config under one namespace: parameters? – ʎɹnɔɹǝW Jan 19 '17 at 13:24
  • did you indent your file properly ? `nacho_image` must be indented with 4 spaces like `database_driver` – ᴄʀᴏᴢᴇᴛ Jan 19 '17 at 13:26
  • Yes, it is indented properly otherwise yaml would throw out an error, nacho_image is another symfony bundle so maybe it is not posible to share other bundles parameters – ʎɹnɔɹǝW Jan 19 '17 at 13:27
  • all your parameters need to be defined in the global file in `app/config/parameters.yml` – ᴄʀᴏᴢᴇᴛ Jan 19 '17 at 13:30
  • Yes, they are all in `/app/config/parameters.yml` I am not using anything else, it is the same file where my database credientials are stored – ʎɹnɔɹǝW Jan 19 '17 at 13:32
  • can you paste the content of this file in your question ? – ᴄʀᴏᴢᴇᴛ Jan 19 '17 at 13:33
  • I don't know what is happening, it is a really simple file http://pastebin.com/b0s1egxk – ʎɹnɔɹǝW Jan 19 '17 at 13:34
  • ok so that is what @KhorneHoly and I said : you have not indented your parameter properly you have to indent like this : http://pastebin.com/zWWaiWcB otherwise, symfony will not know it is a parameter – ᴄʀᴏᴢᴇᴛ Jan 19 '17 at 13:36
  • You don't understand I can not put nacho_image within the parameters level, because the bundle nacho_image will not find it, so it stays where it is and I just want to get the it's contens in my controller. In short your solution will work on how to get the parameter inside my controller, but it will break the bundle that depends on it – ʎɹnɔɹǝW Jan 19 '17 at 13:39
  • Anyway, thaks for the help I understand now it is possible but not simple – ʎɹnɔɹǝW Jan 19 '17 at 13:44
1

You're getting this error, because your .yml file isn't well formatted. Your nacho_image parameter isn't within the range of the parameters part, but on the same level, so you can't access it over the parameters.

Edit your parameters shown as below, then you can access your parameters.

parameters:
    database_driver:   pdo_mysql
    ...
    nacho_image: 
        upload: true
        progress: false 
        key: secret_id

For everything else look @CROZET's answer.

If you need to access an value from the config, that isn't set within the parameters section, you could do it the way that is described in this answer.

Community
  • 1
  • 1
KhorneHoly
  • 4,666
  • 6
  • 43
  • 75