I
I
Igor Chernyshev2011-11-30 11:42:58
symfony
Igor Chernyshev, 2011-11-30 11:42:58

Symfony 2, reading yml config from controllers or services?

Good afternoon.
I decided to quietly crawl to Symfony2, and now a question arose, which so far has not been possible to solve on my own.
How to read data from configuration files (config.yml) in the controller, or in the service. Now I'm trying to do this:
1. In the config I write:

test:
  bb: 1

2. In the controller: I get an error in response. about what he can't find.
echo $this->container->getParameter('test.bb');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
png, 2011-12-06
@png

the same.
However, using a container inside services is not correct from an architectural point of view. it is more correct to pass parameters through the constructor or set methods.
The main dock is here: symfony.com/doc/current/book/service_container.html
by the parameters themselves:
in the sevices.yml file - also in any other (not necessarily services) there
can be a line in the config.yml file, there can also be such lines and there is also a paremeters.ini file and there are also environment variables. they can also be accessed through the parameters. if you want the parameter to get into the service constructor, then you specify its name , example, from off-line documentation:
parameters:
test: 1

test = 1

%test%
# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
my_mailer.class: Acme\HelloBundle\Mailer
my_mailer.transport: sendmail
services:
my_mailer:
class: %my_mailer.class%
arguments: [%my_mailer.transport%]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question