Reputation: 2279
I have created a service where I need to read few parameters from parameters.yml, so far I am able to read parameters through container only, I have injected container into my service, I know it's not good practice to inject container instead I should be injecting required services/components,however I am not able to figure out the way to read parameters from parameters.yml without injecting container into service and then reading parameters with the help of container. Can you please guide me for an alternative solution?
EDIT:
I have a long list of parameters, around 12-15, I am storing rate limit options for API in parameters.yml so that they can be easily configured
Thanks.
Upvotes: 3
Views: 1884
Reputation: 10890
Since you have a long list a parameters injecting these one by one would be harsh but you can inject array of parameters.
parameters.yml
parameters:
array_name:
param1: value1
param2: value2
...
services.yml
services:
your_service:
class: Acme\DemoBundle\Class
arguments: [%array_name%]
your service:
public function __construct(array $parameters)
{
//access $parameters['param1'], $parameters['param2']
}
Upvotes: 5
Reputation: 1183
You can pass parameter into your service actually. Like this:
service_name:
class: <pathToClass>
arguments:
- %<parameter-name>%
Upvotes: 0
Reputation: 39370
You can simply pass it in the definition of the service. As example in this xml definition:
<service id="amce.example_service" class="Acme\DemoBundle\Service\Example">
<argument type="service" id="templating" />
<argument>%my_from_parameters%</argument>
</service>
Hope this help
Upvotes: 1