Download the PHP package christianblos/symfony-di-annotation without Composer
On this page you can find all versions of the php package christianblos/symfony-di-annotation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download christianblos/symfony-di-annotation
More information about christianblos/symfony-di-annotation
Files in christianblos/symfony-di-annotation
Package symfony-di-annotation
Short Description Allows you to configure symfony dependency injections via annotations
License MIT
Informations about the package symfony-di-annotation
What's this?
This library is an extension of the Symfony DependencyInjection Component. It allows you to configure the DI Container using annotations directly in your classes.
Set up
1. Install via composer:
composer require christianblos/symfony-di-annotation
2. Add compiler pass to the ContainerBuilder
(see symfony documentation for more information about the ContainerBuilder)
Basic Usage
Just add the @Service
annotation to all of your services and they will be registered to the DIC automatically:
Now you can simply retrieve the service from the container:
Note: You can also add multiple
@Service
annotations if you want to configure two services of the same class. Then you also need to set the id of the service like@Service(id="myService")
because you can not have two services with the same id (By default, the id is the full class name).
Inject params
You can also inject params by adding it to the annotation:
Modify container
It is possible to modify the whole container by using method annotations. One possible use case is an event listener. Depending on your implementation it could look like this:
Note: This is just an example of what is possible. The "ListenTo" Annotation is not implemented in this library! See the example of how you can implement it.
More examples
You can find some examples in the examples folder.
All versions of symfony-di-annotation with dependencies
doctrine/annotations Version ~1.0
symfony/dependency-injection Version >=4.0
symfony/config Version >=4.0