skip to Main Content

I have a line in a sample Symfony app that reads:

$seo = $this->get('sonata.seo.page');

However the config.yml file reads:

sonata_seo:
page:
    metas:
        property: ... etc ...

I’ve read http://symfony.com/doc/current/service_container.html but I’m not clear how exactly the get('sonata.seo.page') works. Does it somehow equate to the key / values in the config file? i.e. does the underscore in sonata_seo get magically changed to a period?

4

Answers


  1. You cannot access values in config.yml direcly, like values in parameters.yml.

    That file can store configuration values for bundles thought.

    Read more here

    Login or Signup to reply.
  2. What it is ‘getting’ in this instance, usually within a controller action, is a Symfony Service.

    In this instance, sonata.seo.page is a reference to a service, setup in the sonata-project/seo-bundle, which returns an instance of the SeoPage class. Normally, this information is set within your local configuration file (config.yml, or a file that it includes), but the service returns the class that allows you to change the values at runtime.

    Login or Signup to reply.
  3. No service are (directly) defined inside config.yml. It’s the bundle that define the service. With $this->get('sonata.seo.page'); you get those.

    The config.yml file it’s just used to customize the bundles.

    Login or Signup to reply.
  4. The SeoBundle defines the semantic configuration section sonata_seo from config.yml and registers the own Extension of DI container.

    Extension implements the load() method. Configuration values from sonata_seo are passed as its first argument.
    Method loads various resources including service definition from Resources/config/service.xml:

    <parameter key="sonata.seo.page.default.class">SonataSeoBundleSeoSeoPage</parameter>
    ...
    <service id="sonata.seo.page.default" class="%sonata.seo.page.default.class%"/>
    

    Next, extension set up sonata.seo.page definition with given configuration parameters.
    This process will be invoked during the container compilation, when the service definition and its settings will be embedded in the container. Result of this process you can find in the dumped container in the cache directory.

    This is a typical scheme of work for the Symfony bundles: define the configuration structure, make an extension, set up the services.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search