How to access a service container in a symfony2 global helper function (service)?

This question started with me, not understanding why I could not pass variables to the symfony2 (service) global helper function, but thanks to people brighter than me, I realized that my mistake was trying to use security_context from inside the class that it did not enter so ...

This is the end result, the code that works. I have not found a better way to make this useful for communication.

Here's how you can get user and other data from security_context from a global function or helper function in symfony2.

I have the following class and function:

<?php namespace BizTV\CommonBundle\Helper; use Symfony\Component\DependencyInjection\ContainerInterface as Container; class globalHelper { private $container; public function __construct(Container $container) { $this->container = $container; } //This is a helper function that checks the permission on a single container public function hasAccess($container) { $user = $this->container->get('security.context')->getToken()->getUser(); //do my stuff } } 

... is defined as a service (in app / config / config.yml), like this ...

 #Registering my global helper functions services: biztv.helper.globalHelper: class: BizTV\CommonBundle\Helper\globalHelper arguments: ['@service_container'] 

Now in my controller, I call this function as follows:

 public function createAction($id) { //do some stuff, transform $id into $entity of my type... //Check if that container is within the company, and if user has access to it. $helper = $this->get('biztv.helper.globalHelper'); $access = $helper->hasAccess($entity); 
+47
php symfony service
Aug 21 '12 at 13:40
source share
5 answers

I assume that the first error (undefined property) occurred before you added the property and constructor. Then you got a second error. This other error means that your constructor expects to receive a Container object, but received nothing. This is due to the fact that when you defined your service, you did not tell the dependency manager that you want to receive the container. Change the service definition as follows:

 services: biztv.helper.globalHelper: class: BizTV\CommonBundle\Helper\globalHelper arguments: ['@service_container'] 

Then the constructor should expect an object of type Symfony \ Component \ DependencyInjection \ ContainerInterface;

 use Symfony\Component\DependencyInjection\ContainerInterface as Container; class globalHelper { private $container; public function __construct(Container $container) { $this->container = $container; } 
+77
Aug 21 2018-12-12T00:
source share

An approach that always works, despite the fact that it is not the best practice in OO

 global $kernel; $assetsManager = $kernel->getContainer()->get('acme_assets.assets_manager');‏ 
+20
Apr 01 '14 at 21:40
source share

Another option is to expand ContainerAware:

 use Symfony\Component\DependencyInjection\ContainerAware; class MyService extends ContainerAware { .... } 

which allows you to call setContainer in a service declaration:

 foo.my_service: class: Foo\Bundle\Bar\Service\MyService calls: - [setContainer, [@service_container]] 

You can then reference the container in your service as follows:

 $container = $this->container; 
+7
Oct 24 '15 at 13:37
source share

This may not be the best way, but what I'm doing is passing the container to the class, so I get it every time I need it.

 $helpers = new Helpers(); or $helpers = new Helpers($this->container); /* My Class */ class Helpers { private $container; public function __construct($container = null) { $this->container = $container; } ... } 

It works every time for me.

+1
Apr 08 '16 at 10:13
source share

You should not introduce service_container in your services. In your example, you should instead add the old security.context or later security.token_storage . See, for example, the section "Avoiding container-specific code" at http://symfony.com/doc/current/components/dependency_injection.html .

Example:

 <?php namespace BizTV\CommonBundle\Helper; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; class globalHelper { private $securityTokenStorage; public function __construct(TokenStorage $securityTokenStorage) { $this->securityTokenStorage= $securityTokenStorage; } public function hasAccess($container) { $user = $this->securityTokenStorage->getToken()->getUser(); //do my stuff } } 

application / Config / config.yml:

 services: biztv.helper.globalHelper: class: BizTV\CommonBundle\Helper\globalHelper arguments: ['@security.token_storage'] 

Your controller:

 public function createAction($id) { $helper = $this->get('biztv.helper.globalHelper'); $access = $helper->hasAccess($entity); 
+1
Oct 27 '16 at 2:59
source share



All Articles