Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Last active February 7, 2019 12:02
Show Gist options
  • Save ajaxray/d1b8fcf7e4ba983b9668 to your computer and use it in GitHub Desktop.
Save ajaxray/d1b8fcf7e4ba983b9668 to your computer and use it in GitHub Desktop.

Revisions

  1. ajaxray revised this gist Jun 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symfony2-bundle-config.md
    Original file line number Diff line number Diff line change
    @@ -139,4 +139,4 @@ class SillyManager

    Hope this helps!

    Collected From [an StackOverflow answer](http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml) and saved here from own refenence.
    > Collected From [an StackOverflow answer](http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml) and saved (with some modification) here from own refenence.
  2. ajaxray revised this gist Jun 14, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion symfony2-bundle-config.md
    Original file line number Diff line number Diff line change
    @@ -136,7 +136,6 @@ class SillyManager
    - [How to Load Service Configuration inside a Bundle](http://symfony.com/doc/current/cookbook/bundles/extension.html)
    - [How to Create Friendly Configuration](http://symfony.com/doc/current/cookbook/bundles/configuration.html)
    - [Defining and Processing Configuration Values](http://symfony.com/doc/current/components/config/definition.html)
    -

    Hope this helps!

  3. ajaxray revised this gist Jun 14, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions symfony2-bundle-config.md
    Original file line number Diff line number Diff line change
    @@ -131,6 +131,13 @@ class SillyManager
    }
    ```

    ### Related references

    - [How to Load Service Configuration inside a Bundle](http://symfony.com/doc/current/cookbook/bundles/extension.html)
    - [How to Create Friendly Configuration](http://symfony.com/doc/current/cookbook/bundles/configuration.html)
    - [Defining and Processing Configuration Values](http://symfony.com/doc/current/components/config/definition.html)
    -

    Hope this helps!

    Collected From [an StackOverflow answer](http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml) and saved here from own refenence.
  4. ajaxray revised this gist Jun 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symfony2-bundle-config.md
    Original file line number Diff line number Diff line change
    @@ -133,4 +133,4 @@ class SillyManager

    Hope this helps!

    Collected From [http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml][an StackOverflow answer] and saved here from own refenence.
    Collected From [an StackOverflow answer](http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml) and saved here from own refenence.
  5. ajaxray revised this gist Jun 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symfony2-bundle-config.md
    Original file line number Diff line number Diff line change
    @@ -133,4 +133,4 @@ class SillyManager

    Hope this helps!

    Collected From [http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml](an StackOverflow answer) and saved here from own refenence.
    Collected From [http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml][an StackOverflow answer] and saved here from own refenence.
  6. ajaxray revised this gist Jun 14, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions symfony2-bundle-config.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    FIRST APPROACH: getting it as a parameter
    APPROACH 1: Getting it as a parameter
    -------------------------------------------

    With an extension (more on extensions here) you can keep this easily "separated" into different blocks in the config.yml and then inject that as a parameter gettable from the controller.
    @@ -50,7 +50,7 @@ Then you can get the config from your controller, as you desired in your origina
    $recipient = $this->container->getParameter( 'my_nice_project.contact_email' );
    ```

    SECOND APPROACH: Injecting the config into a service
    APPROACH 2: Injecting the config into a service
    -----------------------------------------------------------------------------

    For readers looking for something similar but for getting the config from a service, there is even a nicer way that never clutters the "paramaters" common space and does even not need the container to be passed to the service (passing the whole container is practice to avoid).
    @@ -133,4 +133,4 @@ class SillyManager

    Hope this helps!

    Collected From (an StackOverflow answer)[http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml] and saved here from own refenence.
    Collected From [http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml](an StackOverflow answer) and saved here from own refenence.
  7. ajaxray renamed this gist Jun 14, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.md → symfony2-bundle-config.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    FIRST APPROACH: Separated config block, getting it as a parameter
    ----------------------------------------------------------------
    FIRST APPROACH: getting it as a parameter
    -------------------------------------------

    With an extension (more on extensions here) you can keep this easily "separated" into different blocks in the config.yml and then inject that as a parameter gettable from the controller.

    @@ -50,7 +50,7 @@ Then you can get the config from your controller, as you desired in your origina
    $recipient = $this->container->getParameter( 'my_nice_project.contact_email' );
    ```

    SECOND APPROACH: Separated config block, injecting the config into a service
    SECOND APPROACH: Injecting the config into a service
    -----------------------------------------------------------------------------

    For readers looking for something similar but for getting the config from a service, there is even a nicer way that never clutters the "paramaters" common space and does even not need the container to be passed to the service (passing the whole container is practice to avoid).
  8. ajaxray created this gist Jun 14, 2015.
    136 changes: 136 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    FIRST APPROACH: Separated config block, getting it as a parameter
    ----------------------------------------------------------------

    With an extension (more on extensions here) you can keep this easily "separated" into different blocks in the config.yml and then inject that as a parameter gettable from the controller.

    Inside your Extension class inside the DependencyInjection directory write this:
    ```
    class MyNiceProjectExtension 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( 'my_nice_project.contact_email', $processedConfig[ 'contact_email' ];
    // Other stuff like loading services.yml
    }
    ```

    Then in your config.yml, config_dev.yml and so you can set

    ```
    my_nice_project:
    contact_email: [email protected]
    ```

    To be able to process that config.yml inside your MyNiceBundleExtension you'll also need a Configuration class in the same namespace:

    ```
    class Configuration implements ConfigurationInterface
    {
    public function getConfigTreeBuilder()
    {
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root( 'my_nice_project' );
    $rootNode->children()->scalarNode( 'contact_email' )->end();
    return $treeBuilder;
    }
    }
    ```

    Then you can get the config from your controller, as you desired in your original question, but keeping the parameters.yml clean, and setting it in the config.yml in separated sections:

    ```
    $recipient = $this->container->getParameter( 'my_nice_project.contact_email' );
    ```

    SECOND APPROACH: Separated config block, injecting the config into a service
    -----------------------------------------------------------------------------

    For readers looking for something similar but for getting the config from a service, there is even a nicer way that never clutters the "paramaters" common space and does even not need the container to be passed to the service (passing the whole container is practice to avoid).

    This trick above still "injects" into the parameters space your config.

    Nevertheless, after loading your definition of the service, you could add a method-call like for example setConfig() that injects that block only to the service.

    For example, in the Extension class:
    ```
    class MyNiceProjectExtension extends Extension
    {
    public function load( array $configs, ContainerBuilder $container )
    {
    $configuration = new Configuration();
    $processedConfig = $this->processConfiguration( $configuration, $configs );
    // Do not add a paramater now, just continue reading the services.
    $loader = new YamlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config' ) );
    $loader->load( 'services.yml' );
    // Once the services definition are read, get your service and add a method call to setConfig()
    $sillyServiceDefintion = $container->getDefinition( 'my.niceproject.sillymanager' );
    $sillyServiceDefintion->addMethodCall( 'setConfig', array( $processedConfig[ 'contact_email' ] ) );
    }
    }
    ```

    Then in your services.yml you define your service as usual, without any absolute change:

    ```
    services:
    my.niceproject.sillymanager:
    class: My\NiceProjectBundle\Model\SillyManager
    arguments: []
    ```

    And then in your SillyManager class, just add the method:
    ```
    class SillyManager
    {
    private $contact_email;
    public function setConfig( $newConfigContactEmail )
    {
    $this->contact_email = $newConfigContactEmail;
    }
    }
    ```

    Note that this also works for arrays instead of scalar values! Imagine that you configure a rabbit queue and need host, user and password:
    ```
    my_nice_project:
    amqp:
    host: 192.168.33.55
    user: guest
    password: guest
    ```

    Of course you need to change your Tree, but then you can do:
    ```
    $sillyServiceDefintion->addMethodCall( 'setConfig', array( $processedConfig[ 'amqp' ] ) );
    ```
    and then in the service do:
    ```
    class SillyManager
    {
    private $host;
    private $user;
    private $password;
    public function setConfig( $config )
    {
    $this->host = $config[ 'host' ];
    $this->user = $config[ 'user' ];
    $this->password = $config[ 'password' ];
    }
    }
    ```

    Hope this helps!

    Collected From (an StackOverflow answer)[http://stackoverflow.com/questions/4821692/how-do-i-read-configuration-settings-from-symfony2-config-yml] and saved here from own refenence.