2013-07-30 25 views
7

Tôi nhận được dưới đây lỗi,Lỗi: Bạn không thể tạo một dịch vụ ("templating.helper.assets") của một phạm vi hoạt động ("yêu cầu")

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

Tôi render mẫu cành lá từ symfony 2 tùy chỉnh console lệnh

Dưới đây là lớp dịch vụ của tôi là thuê bao sự kiện, tôi kích hoạt sự kiện onCommentAddEmail bởi giao diện điều khiển lệnh symfony để gửi 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) 
      ); 

    ....... 


    } 

} 

$ cơ thể được truyền cho swiftmailer gửi Một email.

Đây là, định nghĩa dịch vụ của tôi,

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> 

Dưới đây bài nói vấn đề được cố định trong symfony 2.1, nhưng tôi vẫn nhận được lỗi,

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

Tôi đã được xác nhận là http://symfony.com/doc/current/cookbook/service_container/scopes.html, tôi đã chuyển toàn bộ vùng chứa với dịch vụ của tôi.

+0

Hãy nhìn vào phản ứng đối với một vấn đề tương tự (http://stackoverflow.com/a/24409012/1263890) –

Trả lời

26

Không chắc chắn nếu nó là cách tốt nhất nhưng thêm này làm việc cho tôi,

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

Theo trích dẫn của 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

dịch, bạn không thể sử dụng asset chức năng bên trong mẫu của bạn nếu bạn đang có ý định sử dụng nó từ CLI.

+1

Whats giải pháp sau đó ? – vishal

+0

Bạn có thể thử thay thế tất cả nội dung ('img/test.png') bằng http: //mywebsite/img/test.png. –

+0

Và đơn vị kiểm tra nội dung của các mẫu Twig thì sao? – Rvanlaak

1

vấn đề nảy sinh vì bạn sử dụng tài sản() trong mẫu của bạn, mà phụ thuộc vào Yêu cầu, và có không phải là Yêu cầu trong ứng dụng dòng lệnh.

Nhanh chóng khắc phục:

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

More đây: https://stackoverflow.com/a/24382994/1851915

Các vấn đề liên quan