Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject a service in another service in Symfony?

Tags:

php

symfony

I am trying to use the logging service in another service in order to trouble shoot that service.

My config.yml looks like this:

services:     userbundle_service:         class:        Main\UserBundle\Controller\UserBundleService         arguments: [@security.context]      log_handler:         class: %monolog.handler.stream.class%         arguments: [ %kernel.logs_dir%/%kernel.environment%.jini.log ]       logger:         class: %monolog.logger.class%         arguments: [ jini ]         calls: [ [pushHandler, [@log_handler]] ] 

This works fine in controllers etc. however I get no out put when I use it in other services.

Any tips?

like image 859
jini Avatar asked Aug 03 '12 18:08

jini


People also ask

What is dependency injection Symfony?

The Symfony DependencyInjection component provides a standard way to instantiate objects and handle dependency management in your PHP applications. The heart of the DependencyInjection component is a container which holds all the available services in the application.

Are Symfony services Singleton?

symfony2 service is not a singleton.

What is Symfony Service Container?

In Symfony, these useful objects are called services and each service lives inside a very special object called the service container. The container allows you to centralize the way objects are constructed. It makes your life easier, promotes a strong architecture and is super fast!


2 Answers

You pass service id as argument to constructor or setter of a service.

Assuming your other service is the userbundle_service:

userbundle_service:     class:        Main\UserBundle\Controller\UserBundleService     arguments: [@security.context, @logger] 

Now Logger is passed to UserBundleService constructor provided you properly update it, e.G.

protected $securityContext; protected $logger;  public function __construct(SecurityContextInterface $securityContext, Logger $logger) {     $this->securityContext = $securityContext;     $this->logger = $logger; } 
like image 196
Inoryy Avatar answered Sep 29 '22 05:09

Inoryy


For Symfony 3.3, 4.x, 5.x and above, the easiest solution is to use Dependency Injection

You can directly inject the service into another service, (say MainService)

// AppBundle/Services/MainService.php // 'serviceName' is the service we want to inject public function __construct(\AppBundle\Services\serviceName $injectedService)  {     $this->injectedService = $injectedService; } 

Then simply, use the injected service in any method of the MainService as

// AppBundle/Services/MainService.php public function mainServiceMethod() {     $this->injectedService->doSomething(); } 

And viola! You can access any function of the Injected Service!

For older versions of Symfony where autowiring does not exist -

// services.yml services:     \AppBundle\Services\MainService:         arguments: ['@injectedService'] 
like image 25
Niket Pathak Avatar answered Sep 29 '22 04:09

Niket Pathak