我刚刚拉了一个全力以赴试图让它发挥作用,我可能错过了一些愚蠢的东西,但帮助我。
我有一个发送电子邮件的symfony2服务。我已将@templating
服务注入到我的服务构造函数中,如下所示:
order_service:
class: AppBundle\Services\OrderService
arguments: [ "@doctrine.orm.entity_manager", "@mailer", "@templating" ]
/**
* @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\".
所以,这是我尝试过的总结:
app/Resources/views
->render()
有什么想法吗?
答案 0 :(得分:4)
即使您的服务与您的模板位于同一名称空间,但这并不意味着您可以直接调用它。您是否尝试过渲染普通控制器模板的方式,如下所示:
->setBody($this->twig->render(
'AppBundle:Emails:email-voucher.html.twig', [
'order' => $order,
'voucher' => $voucher,
'customer' => $customer
]
), 'text/html');