0

I have my class MyTwigExtension extends \Twig_Extension, where i have created my filters for twig. I want to use a service in the implementation of a new filter. Something like this:

$canView = $this->get('security_service')
                ->hasClassPermission(
                        'MyBundle:MyEntity',
                        $this->get('security.context')->getToken()->getUser(), 
                        array(MaskBuilder::MASK_VIEW)
                 );

Into a controller this work fine, but outside it...

Veve
  • 6,643
  • 5
  • 39
  • 58
Oriam
  • 641
  • 10
  • 19

1 Answers1

0

Check this answer I gave on dependency injection outside a controller here.

Then check this link to see how to register your TwigExtension as a service.

Just a quick edit, sometimes, when you do TwigExtension as a service, you can't inject directly some service because it cause a ScopeWideningException. If you get this exception, then you can add scope = request to you service definition. If it still throws an exception, inject the whole dependency container and do $this->container->get('service.id.here') to retrieve the services you need.

Hope it helps.

Regards,
Matt

Community
  • 1
  • 1
Matt
  • 10,633
  • 3
  • 46
  • 49