Search code examples
symfonysymfony-forms

Symfony2: how to get config parameters in Form classes


If I am inside a controller, I can easily read the config parameters using:

$this->container->getParameter('profession');

But when I am in some other class, say a Form type, how can I get hold of the config parameters?

$container = new Container(); 
$container->getParameter('profession');

The above code shouldn't and doesn't work.


Solution

  • Another similar solution is make your form type a service and inject the needed parameters. Then all your controller needs to do is to grab the service. Surround the parameter name with percent signs.

    In services.xml

        <service
            id     = "zayso_area.account.create.formtype"
            class  = "Zayso\AreaBundle\Component\FormType\Account\AccountCreateFormType"
            public = "true">
            <argument type="service" id="doctrine.orm.accounts_entity_manager" />
            <argument type="string">%zayso_core.user.new%</argument>
        </service>
    

    And if you really wanted to then you could inject the complete container though that is discouraged.