Symfony - How to set alias for a service using a parameter's value
Asked Answered
M

2

6

I want to set an alias for a service in order to be able to use different services in different environments. So, here is the services.yaml

.
.
.
register_request:
        alias: %register_request_service%
main_register_request:
        class: AppBundle\Services\Requests\RegisterRequest
null_register_request:
        class: AppBundle\Services\Requests\NullRegisterRequest
.
.
.

and in my development environment the parameters.yml looks like:

.
.
.
    register_request_service: null_register_request
.
.
.

But it does not work! when I use clear:cache it says:

  [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
  Unable to replace alias "%register_request_service%" with "register_request".

  [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
  The service definition "%register_request_service%" does not exist.

Any Idea about what I'm missing?

Mclane answered 14/1, 2016 at 9:40 Comment(0)
M
3

Elnur answer works but I decided to fix the problem this way:

I have created a services config file for dev environment (services_dev.yml) and did override the service in it. So, the services.yml looks like:

# app/config/services.yml
# ...
services:
    # ...
    register_request:
            class: AppBundle\Services\Requests\RegisterRequest
    # ...

and the services_dev.yml looks like:

# app/config/services_dev.yml
services:
    register_request:
            class: AppBundle\Services\Requests\NullRegisterRequest

Then, I have imported the services_dev.yml in in config_dev.yml

# app/config/config_dev.yml
imports:
    - { resource: config.yml }
    - { resource: services_dev.yml }

# ...

This way the application setup process does not need the extra information about those classes' name.

Mclane answered 16/1, 2016 at 8:58 Comment(0)
A
2

It's easier to just define the class name as a parameter. That's the idiom used a lot in Symfony itself and its bundles.

Austroasiatic answered 16/1, 2016 at 6:14 Comment(4)
I agree with Alireza's answer. In the best practices document, it says that "Don't define parameters for the classes of your services." While I agree that you shouldn't follow any document blindly to the tee, having environment-based services means someone installing your web application doesn't constantly have to define a value in parameters.yml.Tavie
Did I say anything about parameters.yml?Austroasiatic
Nope. You said nothing about where to define parameters, and the usual place for defining parameters is in the file that is named after them. Your answer is only one paragraph so I have to guess most of what you mean; sorry if my guesses fall short.Tavie
@ElnurAbdurrakhimov Do you by any chance remember where you saw this being used in practice in the Symfony source code? I was thinking of doing something similar, but was hoping to use an alias instead of the FQCN.Roebuck

© 2022 - 2024 — McMap. All rights reserved.