Overdose
Overdose

Reputation: 585

Symfony : How to access configuration in controller

This may be a silly question, but i can't see how to access this data :

In the main app/config/config.yml, i have the general configuration data for my application.

parameters:    #accessible by getParameter()
    locale: fr
    # ...
fos_user:    #accessible by ???
    #...
    registration:
        form:
            from_email:
                address:     [email protected]
                sender_name: TheSenderName

In my custom bundle i can access the parameters from that config.yml file with :

$this->container->getParameter('locale'); //gives me "fr" as expected

But how can i access the non parameters configuration values ?

i would like here to get the adress defined in te FOS User bundle configuration : I can't do

$admin_adress = $this->container->getParameter('fos_user.registration.confirmation.from_email.address');

What is the right way to access thoses ?

Edit : Yes i wanted to access this config data from FOS User Bundle (here in the example, but it could be any), inside a controller or whatever in my bundle.

Upvotes: 2

Views: 6060

Answers (3)

DonCallisto
DonCallisto

Reputation: 29922

You can act that way

parameters:
    locale: fr
    fos_user:
        registration:
            form:
                from_email:
                    address: [email protected]
    # ...
fos_user:
    #...
    registration:
        form:
            from_email:
                address:     %fos_user.registration.form.from_email.address%
                sender_name: TheSenderName

Of course I've choose this name just to fit your controller request for parameter: you can (and maybe should) choose other keys.

However with container->getParameter you can access only parameters section of your configuration file (plus parameters exposed from vendors)

Upvotes: 0

goto
goto

Reputation: 8162

I think this question nailed it

class MyProjectExtension extends Extension
{
    public function load( array $configs, ContainerBuilder $container )
    {
        // The next 2 lines are pretty common to all Extension templates.
        $configuration = new Configuration();
        $processedConfig = $this->processConfiguration( $configuration, $configs );

        // This is the KEY TO YOUR ANSWER
        $container->setParameter( 'from_email.address', $processedConfig[ 'registration.confirmation.from_email.address' ];

        // Other stuff like loading services.yml
    }

Upvotes: 2

LondonUnderground
LondonUnderground

Reputation: 89

One of the solutions can be passing your controller as a service, then inject your parameters like that :

# services.yml
my.amazing.controller:
    class: CompanyFirst\LabBundle\Controller\PloufController
    arguments:
        - '@filesystem'
        - '%dump_file_convention%' // your config parameter

Upvotes: 0

Related Questions