Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 457
A.Alinia Avatar asked Jan 14 '16 09:01

A.Alinia


2 Answers

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.

like image 183
A.Alinia Avatar answered Oct 09 '22 14:10

A.Alinia


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.

like image 22
Elnur Abdurrakhimov Avatar answered Oct 09 '22 15:10

Elnur Abdurrakhimov