Example: create a custom Monolog channel and inject the service in controller (Symfony 4).
I was thinking that I will have to create some sort of custom service definition in order to inject the service "monolog.logger.mychannel" into my controller, but no, it was way easier.
In services.yaml just add:
services: # default configuration for services in *this* file _defaults: bind: # pass this service to any $mychannelLogger argument for any # service that's defined in this file $mychannelLogger: '@monolog.logger.mychannel'
Now is super easy to inject the service in controllers by adding "$mychannelLogger" in the list of function parameters:
/** * @Route("/{id}", name="project_update", methods={"POST"}) */ public function delete(Request $request, LoggerInterface $mychannelLogger): Response {
Read more about it in Symfony docs: https://symfony.com/doc/current/service_container.html#services-binding
No comments:
Post a Comment