简体   繁体   English

Symfony2服务无法找到模板

[英]Symfony2 service unable to find template

I've just pulled an all-nighter trying to get this to work, and I'm probably missing something foolish, but help me out. 我刚刚通宵达旦地试图使它正常工作,我可能缺少一些愚蠢的东西,但请帮帮我。

I have a symfony2 service which sends emails. 我有一个symfony2服务,可以发送电子邮件。 I have injected the @templating service into my service's constructor like so: 我已经将@templating服务注入到服务的构造函数中,如下所示:

services.yml services.yml

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

OrderService.php 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;
}

I have my templates located in AppBundle/Resources/views/Emails . 我的模板位于AppBundle/Resources/views/Emails Further down in my service is the function that tries to send 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);
    }
}

I always get the error Unable to find template \\"Emails/email-voucher.html.twig\\". 我总是收到错误消息Unable to find template \\"Emails/email-voucher.html.twig\\".

So, here's a summary of what I've tried: 因此,这是我尝试过的内容的摘要:

  • Basically everything suggested on similar StackOverflow questions 基本上所有关于类似StackOverflow问题的建议
  • Moving templates to app/Resources/views 将模板移至app/Resources/views
  • Every path/relative path I can think of in the call to ->render() 我可以在调用->render()想到的每条路径/相对路径
  • Pulling my hair out 拉我的头发

Any ideas? 有任何想法吗?

Even if your service lies in the same namespace as it's your template, that does not mean that you can call it directly like that. 即使您的服务位于与模板相同的名称空间中,这也不意味着您可以像这样直接调用它。 Did you tried the way you render normal controller templates, like this: 您是否尝试过渲染常规控制器模板的方式,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM