crmpicco
crmpicco

Reputation: 17181

What is the correct way to define service definition arguments in a Symfony 3.4 application

What is the correct way to define service definition arguments in a Symfony 3.4 application?

I have noticed all of the following are valid:

AppBundle\Services\RFC\Spackman:
    arguments:
        $watpSdk: '@watp.client'
    calls:
        - [setPlayerName, ['%p_name%']]

AppBundle\Services\RFC\Gordon:
    $client: '@crmpicco.client'

zendesk.client:
    class: Zendesk\API\HttpClient
    arguments:
       - '%zendesk_subdomain%'

The official docs say this, however i'm unsure what the correct standard is:

New in version 3.3: The ability to configure an argument by its name ($adminEmail) was added in Symfony 3.3. Previously, you could configure it only by its index (2 in this case) or by using empty quotes for the other arguments.

Upvotes: 0

Views: 355

Answers (1)

Stefmachine
Stefmachine

Reputation: 392

  • When you are using hyphens, it feeds the arguments in order to the constructor/factory of your service.
  • When you use "$argname", it feeds the arguments based on the name of the constructor/factory parameter.
  • You can also use "index_#" to feed the argument by specific index.

Any of them are proper ways of doing it. I prefer to use hyphens since, if you refactor the parameter's name someday, you won't have to refactor it in your configuration too.

I also use "index_#" when I need to extend service definition and replace one of the arguments defined in the parent service.

It's a matter of taste, since service extension can be completely ignored.

Upvotes: 1

Related Questions