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; }
... 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);
php symfony service
Matt Welander Aug 21 '12 at 13:40 2012-08-21 13:40
source share