7

エラーを下回っていますが、

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

symfony 2 カスタム コンソール コマンドから twig テンプレートをレンダリングしています

以下は、イベント サブスクライバーである私のサービス クラスです。メールを送信するために symfony コンソール コマンドで onCommentAddEmail イベントをトリガーしています。

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 はメールを送信するために swiftmailer に渡されます。

これが私のサービス定義です。

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>

以下の投稿では、問題は symfony 2.1 で修正されたと書かれていますが、まだエラーが発生しています。

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

私はすでにhttp://symfony.com/doc/current/cookbook/service_container/scopes.htmlを参照しており、コンテナー全体をサービスに渡しました。

4

3 に答える 3

1

Requestに依存するテンプレートでasset()を使用し、コマンドラインアプリにRequestがないために問題が発生します。

クイックフィックス:

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

詳細はこちら: https://stackoverflow.com/a/24382994/1851915

于 2014-06-24T09:28:30.860 に答える