How to provide a container as an argument for services

in my service constructor

public function __construct( EntityManager $entityManager, SecurityContextInterface $securityContext) { $this->securityContext = $securityContext; $this->entityManager = $entityManager; 

I pass entityManager and securityContext as an argument. also my services.xml here

  <service id="acme.memberbundle.calendar_listener" class="Acme\MemberBundle\EventListener\CalendarEventListener"> <argument type="service" id="doctrine.orm.entity_manager" /> <argument type="service" id="security.context" /> 

but now I want to use the container in services such as

 $this->container->get('router')->generate('fos_user_profile_edit') 

How to transfer the container to services?

+25
php symfony
Jun 15 '13 at 17:46
source share
4 answers

Add

 <argument type="service" id="service_container" /> 

And in your listener class:

 use Symfony\Component\DependencyInjection\ContainerInterface; //... public function __construct(ContainerInterface $container, ...) { 
+39
Jun 15 '13 at 17:50
source share

It is easy if the service extends ContainerAware

 use \Symfony\Component\DependencyInjection\ContainerAware; class YouService extends ContainerAware { public function someMethod() { $this->container->get('router')->generate('fos_user_profile_edit') ... } } 

service.yml

  your.service: class: App\...\YouService calls: - [ setContainer,[ @service_container ] ] 
+55
Jun 15 '13 at 18:44
source share

This is 2016 , you can use trait , which will help you expand one class with several libraries.

 <?php namespace iBasit\ToolsBundle\Utils\Lib; use Doctrine\Bundle\DoctrineBundle\Registry; use Symfony\Component\DependencyInjection\ContainerInterface; trait Container { private $container; public function setContainer (ContainerInterface $container) { $this->container = $container; } /** * Shortcut to return the Doctrine Registry service. * * @return Registry * * @throws \LogicException If DoctrineBundle is not available */ protected function getDoctrine() { if (!$this->container->has('doctrine')) { throw new \LogicException('The DoctrineBundle is not registered in your application.'); } return $this->container->get('doctrine'); } /** * Get a user from the Security Token Storage. * * @return mixed * * @throws \LogicException If SecurityBundle is not available * * @see TokenInterface::getUser() */ protected function getUser() { if (!$this->container->has('security.token_storage')) { throw new \LogicException('The SecurityBundle is not registered in your application.'); } if (null === $token = $this->container->get('security.token_storage')->getToken()) { return; } if (!is_object($user = $token->getUser())) { // eg anonymous authentication return; } return $user; } /** * Returns true if the service id is defined. * * @param string $id The service id * * @return bool true if the service id is defined, false otherwise */ protected function has ($id) { return $this->container->has($id); } /** * Gets a container service by its id. * * @param string $id The service id * * @return object The service */ protected function get ($id) { if ('request' === $id) { @trigger_error('The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\\Component\\HttpFoundation\\Request to your controller parameters to retrieve the request instead.', E_USER_DEPRECATED); } return $this->container->get($id); } /** * Gets a container configuration parameter by its name. * * @param string $name The parameter name * * @return mixed */ protected function getParameter ($name) { return $this->container->getParameter($name); } } 

Your facility to be serviced.

 namespace AppBundle\Utils; use iBasit\ToolsBundle\Utils\Lib\Container; class myObject { use Container; } 

Service Settings

  myObject: class: AppBundle\Utils\myObject calls: - [setContainer, ["@service_container"]] 

Call your service in the controller

 $myObject = $this->get('myObject'); 
+13
Jan 28 '16 at 1:18
source share

If all your services are ContainerAware , I suggest creating a BaseService class that will contain all the common code with your other services.

1) Create a class Base\BaseService.php :

 <?php namespace Fuz\GenyBundle\Base; use Symfony\Component\DependencyInjection\ContainerAware; abstract class BaseService extends ContainerAware { } 

2) Register this service as abstract in your services.yml

 parameters: // ... geny.base.class: Fuz\GenyBundle\Base\BaseService services: // ... geny.base: class: %geny.base.class% abstract: true calls: - [setContainer, [@service_container]] 

3) Now in your other services, your BaseService class is BaseService instead of ContainerAware :

 <?php namespace Fuz\GenyBundle\Services; use Fuz\GenyBundle\Base\BaseService; class Loader extends BaseService { // ... } 

4) Finally, you can use the parent option in the services declaration.

 geny.loader: class: %geny.loader.class% parent: geny.base 

I prefer this way for several reasons:

  • there is consistency between code and configuration
  • this avoids duplication of too many configurations for each service.
  • You have a base class for each service, very useful for general code.
+5
Sep 06 '15 at 16:02
source share



All Articles