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
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
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 thesonata-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.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.
The SeoBundle defines the semantic configuration section
sonata_seo
fromconfig.yml
and registers the own Extension of DI container.Extension implements the
load()
method. Configuration values fromsonata_seo
are passed as its first argument.Method loads various resources including service definition from
Resources/config/service.xml
: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.