Search code examples
phpsymfonysymfony-console

How to retrieve parameter from parameters.yml outside controller?


I need parameter from parameters.yml in Command\ContainerAwareCommand class. I don't see $this->getParameter() there.

Is there a simple way to retrieve parameter from parameters.yml outside controller?

Simpler than: Write "friendly configuration", and put parameter foo into config.yml, and then retrieve param in DependencyInjection\Extension::load(), and set it: $container->setParameter('foo', $foo), and finally in Command\ContainerAwareCommand retrieve it $this->getContainer()->getParameter('foo')?


Solution

  • You can simply access to the container via:

    ... extends ContainerAwareCommand
    
    ...
    $this->getContainer()->getParameter('my-params');
    

    EDIT:

    You can define your own custom parameters then import in the main config.yml files as example:

    config.yml

    imports:
        - { resource: parameters.yml }
        - { resource: security.yml }
        - { resource: services.yml }
        - { resource: "@ApplicationBundle/Resources/config/parameters.yml" }
    

    hope this help