serviceenvironments

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


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?


Solution

  • 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.