2013-07-30 12 views
7

io sono sempre sotto l'errore,Errore: non è possibile creare un servizio ("templating.helper.assets") di un ambito inattiva ("richiesta")

[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". 

sto il rendering modello ramoscello da symfony 2 personalizzato comando da console

Qui di seguito è la mia classe di servizio, che è l'abbonato evento, sto innescando eventi onCommentAddEmail dal comando della console symfony per inviare e-mail,

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) 
      ); 

    ....... 


    } 

} 

$ corpo è passato a Swiftmailer per inviare un'email.

Questo è il mio servizio di 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> 

Sotto messaggio dice che il problema è stato risolto in symfony 2.1, ma sono ancora errori ottenendo,

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

Ho già arbitrato a http://symfony.com/doc/current/cookbook/service_container/scopes.html, ho passato l'intero contenitore al mio servizio.

+0

Dai un'occhiata alla risposta per un problema simile (http://stackoverflow.com/a/24409012/1263890) –

risposta

26

Non so se è il modo migliore, ma l'aggiunta di questo ha lavorato per me,

$this->container->enterScope('request'); 
    $this->container->set('request', new Request(), 'request'); 
6

come citato da Stof:

if you use the request-based asset helper (getting the base url from the request object), it cannot be used from the CLI indeed, as you don't have a request there

traduzione, non è possibile utilizzare la funzione asset all'interno del vostro modello se avete intenzione di usarlo dalla CLI.

+1

Che cosa è la soluzione poi ? – vishal

+0

Si potrebbe provare a sostituire tutti gli asset ('img/test.png') da http: //mywebsite/img/test.png. –

+0

E per quanto riguarda Unit testare il contenuto dei modelli Twig? – Rvanlaak

1

Il problema nasce dal fatto che si utilizza attività() nel modello, che dipende richiesta, e là è Richiesta nell'app della riga di comando.

correzione rapida:

framework: 
    templating: 
    assets_base_urls: { http: ["http://yoursite.com"], ssl: ["http://yoursite.com"] } 

Maggiori informazioni qui: https://stackoverflow.com/a/24382994/1851915

Problemi correlati