Warning: You are browsing the documentation for version 2.6 which is not maintained anymore.
If some of your projects are still using this version, consider upgrading.
Table of Contents
There are some helpful methods for working with the service definitions.
To find out if there is a definition for a service id:
This is useful if you only want to do something if a particular definition exists.
You can retrieve a definition with:
getDefinition() also resolves aliases so if the
argument is an alias you will get the underlying definition.
The service definitions themselves are objects so if you retrieve a definition with these methods and make changes to it these will be reflected in the container. If, however, you are creating a new definition then you can add it to the container using:
If you need to create a new definition rather than manipulate one retrieved from the container then the definition class is Definition.
First up is the class of a definition, this is the class of the object returned when the service is requested from the container.
To find out what class is set for a definition:
and to set a different class:
$definition->setClass($class); // Fully qualified class name as string
To get an array of the constructor arguments for a definition you can use:
or to get a single argument by its position:
$definition->getArgument($index); // e.g. $definition->getArgument(0) for the first argument
You can add a new argument to the end of the arguments array using:
The argument can be a string, an array, a service parameter by using
or a service id by using:
1 2 3 4 5
use Symfony\Component\DependencyInjection\Reference; // ... $definition->addArgument(new Reference('service_id'));
In a similar way you can replace an already set argument by index using:
You can also replace all the arguments (or set some if there are none) with an array of arguments:
If the service you are working with uses setter injection then you can manipulate any method calls in the definitions as well.
You can get an array of all the method calls with:
Add a method call with:
$method is the method name and
$arguments is an array of the
arguments to call the method with. The arguments can be strings, arrays,
parameters or service ids as with the constructor arguments.
You can also replace any existing method calls with an array of new ones with:
There are more examples of specific ways of working with definitions in the PHP code blocks of the configuration examples on pages such as Using a Factory to Create Services and Managing Common Dependencies with Parent Services.
The methods here that change service definitions can only be used before the container is compiled. Once the container is compiled you cannot manipulate service definitions further. To learn more about compiling the container see Compiling the Container.
There might be use cases when you need to include another file just before the service itself gets loaded. To do so, you can use the setFile() method:
Notice that Symfony will internally call the PHP statement
which means that your file will be included only once per request.