Symfony2 服务找不到模板

Symfony2 service unable to find template

我刚刚通宵达旦试图让它工作,我可能漏掉了一些愚蠢的东西,但请帮帮我。

我有一个发送电子邮件的 symfony2 服务。我已经将 @templating 服务注入到我服务的构造函数中,如下所示:

services.yml

order_service:
    class: AppBundle\Services\OrderService
    arguments: [ "@doctrine.orm.entity_manager", "@mailer", "@templating" ]

OrderService.php

/**
 * @var EntityManager
 */
protected $em;

protected $twig;

protected $mailer;    

public function __construct(EntityManager $_entityManager, $_mailer, $_twig)
{
    $this->em = $_entityManager;
    $this->mailer = $_mailer;
    $this->twig = $_twig;
}

我的模板位于 AppBundle/Resources/views/Emails。在我的服务中更靠下的是尝试发送电子邮件的功能。

protected function send_emails($vendors, $order)
{
    $customer = $order->getCustInfo();

    // Send email(s) to customer
    foreach ($order->getVouchers() as $voucher)
    {
        $message = \Swift_Message::newInstance()
            ->setSubject('Voucher # ' . $voucher->getId())
            ->setFrom('from_address@foobar.com')
            ->setTo($customer->email)
            ->setBody($this->twig->render(
                'Emails/email-voucher.html.twig', [
                    'order'     =>  $order,
                    'voucher'   =>  $voucher,
                    'customer'  =>  $customer
                ]
            ), 'text/html');

        $this->mailer->send($message);
    }
}

我总是得到错误 Unable to find template \"Emails/email-voucher.html.twig\".

所以,这是我尝试过的总结:

有什么想法吗?

即使您的服务位于与模板相同的命名空间中,也不意味着您可以像那样直接调用它。您是否尝试过渲染普通控制器模板的方式,如下所示:

->setBody($this->twig->render(
            'AppBundle:Emails:email-voucher.html.twig', [
                'order'     =>  $order,
                'voucher'   =>  $voucher,
                'customer'  =>  $customer
            ]
        ), 'text/html');