0

I have few parameters related to AWS configuration, I need to get access to from my service. Usually, I'm injecting ParameterBagInterface to the service if I need few parameters and using it to get parameters I need, in such way I do not need to check if a parameter really exists and handle it on my own, as ParameterBag throwing ParameterNotFoundException if a parameter is not exist.

But to make ParameterBag working as I need, parameters in service.yml should be added like:

aws.default_one: '%env(AWS_DEFAULT_BUCKET)%' 
aws.default_two: '%env(AWS_DEFAULT_BUCKET)%'

and now I can get it from bag like this $this->parameterBag->get('aws.default_one')

But yml syntax allowing to add nested parameters, so I can add parameters to config like this:

aws:
    default_one: '%env(AWS_DEFAULT_BUCKET)%' 
    default_two: '%env(AWS_DEFAULT_BUCKET)%'

it looks much cleaner and more readable especially when you have a lot of parameters, but in such a way I can't just write $this->parameterBag->get('aws.default_one') (it will throw the error The parameter "aws.default_one" must be defined ) and the only way to get it from parameters bag I found is $this->parameterBag->get('aws')['default_one'] but then it makes no sense to use parameter bag at all as you still need to make validation by your own to check if default_one key exists and handle it. And in such case instead of passing whole parameter bag, I can just pass %aws% to the service and work with it as with php array.

So questions are:

  • what is the best practice to inject a list of parameters to the service, inject parameter bag or just pass each parameter or array of nested parameters to the service?
  • maybe there is some way to get nested parameter using dot notation instead of doing like $this->parameterBag->get('aws')['default_one'] as it makes life easier with just using parameter bag which throwing exception?

1 Answer 1

1

Usually, parameters are injected and defined in the arguments section of your service :

    App\Service\MyService:
    arguments:
        $config:
            access_token: '%env(resolve:ACCESS_TOKEN)%'
            environment: '%env(resolve:SPEC_ENV)%'

Here, I'm using environment parameters, but it doesn't really matters, you could use plain value. If you need to inject multiple parameters :

    App\Service\MyService:
    arguments:
        $access_token: '%env(resolve:ACCESS_TOKEN)%'
        $environment: '%env(resolve:SPEC_ENV)%'

And your service definition will look like :

public function __construct($access_token, $environment)
{
    //...
}

Also from the documentation :

The used . notation is a Symfony convention to make parameters easier to read. Parameters are flat key-value elements, they can't be organized into a nested array

What you are trying to do is a config tree, which is more appropriate for public bundles : https://symfony.com/doc/current/bundles/configuration.html

Sign up to request clarification or add additional context in comments.

2 Comments

what If I in some service I need get access to many parameters (around 10) and they different per env and number of params can vary, I do not think it's a good idea to add all of them as constructor arguments separately... That why I want to use parameter bag, and asking if it supports access using dot notation. But as I see it can't.... so I need pass whole array as argument and add validation by my self. Thanks!
You could use the env folders, and yes you have to validate manually. And the doc is pretty clear that the dot is only a convention of naming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.