2

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:     mymail@mydomain.fr
                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.

Overdose
  • 585
  • 7
  • 30
  • Are you requiring this for a Bundle you have made? – Adam Harrison Aug 10 '16 at 18:23
  • 1
    Yep. Silly question. Well, not really. Only parameters can be directly accessed via getParameter. Normal configuration stuff will not be available unless the bundle itself makes them available. Your best bet would be to define from_email as a parameter. Or maybe dig into the bundle and see if they are exposed somewhere. – Cerad Aug 10 '16 at 18:24
  • couldn't he set them as parameters using the bundle configuration and extension class ? – Adam Harrison Aug 10 '16 at 19:22
  • Thank you for all your comments and solutions. I see now that it wasn't such a naive question. I nailvely thought that since every bundle have their configuration in config.yml, there was a "regular" way (a SF function) too access any of those in my custom bundle (in a controller, listener, manager or whatever). To me, Don Calisto solution is the easiest one. It's better than duplicating the parameters elsewhere. But still, in the example (FOS User bundle), this is a standard config data value. – Overdose Aug 11 '16 at 11:14
  • I created a bundle for accessing final configuration values of each registered bundle (built-in like the SecurityBundle included). See https://github.com/chalasr/RCHConfigAccessBundle – chalasr Aug 11 '16 at 13:28

3 Answers3

2

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
    }
Community
  • 1
  • 1
goto
  • 7,908
  • 10
  • 48
  • 58
0

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
0

You can act that way

parameters:
    locale: fr
    fos_user:
        registration:
            form:
                from_email:
                    address: mymail@mydomain.fr
    # ...
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)

DonCallisto
  • 29,419
  • 9
  • 72
  • 100