Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: You cannot create a service ("templating.helper.assets") of an inactive scope ("request")

Tags:

twig

symfony

I am getting below error,

  [Twig_Error_Runtime]                                                                                                                                                                                     
  An exception has been thrown during the rendering of a template ("You cannot create a    service ("templating.helper.assets") of an inactive scope ("request").") in "AcmeMessagingBundle:Comment:email.html.twig".

I am rendering twig template from symfony 2 custom console command

Below is my service class which is the event subscriber,I am triggering onCommentAddEmail event by symfony console command to send email,

class NotificationSubscriber implements EventSubscriberInterface
{
     private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }


    public static function getSubscribedEvents()
    {
         return array(
            'comment.add'     => array('onCommentAddEmail', 0),
         );
     }

     public function onCommentAddEmail(CommentAddEvent $event)
     {
              ...................


             $body = $this->container->get('templating')->render(
            'AcmeMessagingBundle:Comment:email.html.twig',
                array('template' => $template)
             );

     .......


    }

}

$body is passed to swiftmailer to send an email.

This is my service defination,

Acme\MessagingBundle\Subscriber\NotificationSubscriber

<services>
    <service id="notification_subscriber" class="%notification_subscriber.class%">
        <argument type="service" id="service_container" />
        <tag name="kernel.event_subscriber" />
    </service>
</services>

Below post says the problem is fixed in symfony 2.1, but I am still getting error,

 https://github.com/symfony/symfony/issues/4514

I have already refereed to http://symfony.com/doc/current/cookbook/service_container/scopes.html, I have passed entire container to my service.

like image 781
vishal Avatar asked Jul 30 '13 09:07

vishal


1 Answers

Not sure if it is the best way but adding this worked for me,

    $this->container->enterScope('request');
    $this->container->set('request', new Request(), 'request');
like image 130
vishal Avatar answered Sep 24 '22 05:09

vishal