我的服务中的RenderView

mon*_*ser 24 php symfony

我是symfony世界的新手.我想在我的服务中使用render,但是我收到了这个错误

调用未定义的方法renderView

我知道renderView是快捷方式

/**
 * Returns a rendered view.
 *
 * @param string $view       The view name
 * @param array  $parameters An array of parameters to pass to the view
 *
 * @return string The rendered view
 */
public function renderView($view, array $parameters = array())
{
    return $this->container->get('templating')->render($view, $parameters);
}
Run Code Online (Sandbox Code Playgroud)

但我不知道我的服务中有什么注射剂.我知道即使有php app/console container:debug命令我可以看到我的所有服务都可用,但我不知道如何采取/选择正确的

更新

我试着补充一下

arguments: [@mailer,@templating]
Run Code Online (Sandbox Code Playgroud)

但我明白了 ServiceCircularReferenceException

UPDATE

我改变了我的service.yml

    arguments: [@service_container]
Run Code Online (Sandbox Code Playgroud)

甚至我的服务

$email = $this->service_container->get('mailer');
$twig = $this->service_container->get('templating');
Run Code Online (Sandbox Code Playgroud)

使用服务邮件(swift)和渲染.

我不认为这是最好的解决方案.我想注射Only mailertemplating

更新杰森的回答之后 我正在使用Symfony 2.3

我的services.yml

services:
    EmailService:
        class: %EmailService.class%
        arguments:  [@mailer,@templating,%EmailService.adminEmail%]
Run Code Online (Sandbox Code Playgroud)

我懂了 ServiceCircularReferenceException

Jas*_*man 26

你是对的renderView(),它只是控制器的快捷方式.使用服务类并注入模板服务时,您所要做的就是将功能更改为render().而不是

return $this->renderView('Hello/index.html.twig', array('name' => $name));
Run Code Online (Sandbox Code Playgroud)

你会用的

return $this->render('Hello/index.html.twig', array('name' => $name));
Run Code Online (Sandbox Code Playgroud)

更新Olivia的回复:

如果您遇到循环引用错误,唯一的方法就是注入整个容器.它不被认为是最佳实践,但有时无法避免.当我不得不诉诸于此时,我仍然在构造函数中设置我的类变量,这样我就可以直接注入它们.所以我会这样做:

use Symfony\Component\DependencyInjection\ContainerInterface;

class MyClass()
{
    private $mailer;
    private $templating;

    public function __construct(ContainerInterface $container)
    {
        $this->mailer = $container->get('mailer');
        $this->templating = $container->get('templating');
    }
    // rest of class will use these services as if injected directly
}
Run Code Online (Sandbox Code Playgroud)

旁注,我刚刚在Symfony 2.5中测试了我自己的独立服务,并没有通过直接注入邮件和模板服务获得循环引用.


Mat*_*usz 24

使用构造函数依赖注入(使用Symfony 3.4测试):

class MyService
{
    private $mailer;
    private $templating;

    public function __construct(\Swift_Mailer $mailer, \Twig_Environment $templating)
    {
        $this->mailer     = $mailer;
        $this->templating = $templating;
    }

    public function sendEmail()
    {
        $message = $this->templating->render('emails/registration.html.twig');

        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

无需配置参数.

  • Twig_Environment: `/** @deprecated 自 Twig 2.7 起,使用“Twig\Environment”代替 */` (3认同)

Day*_*rdo 5

这适用于 Symfony +4.2,假设您的应用程序的命名空间是 App 并且您的邮件程序类服务名为 EmailService。

在您的服务类上:

// ...

private $mailer;
private $templating;

public function __construct( \Swift_Mailer $mailer, \Twig\Environment $templating )
{
    $this->mailer = $mailer;
    $this->templating = $templating;
}

public function sendEmailRegistration()
{
    $message = $this->templating->render('emails/registration.html.twig');

    // ...
}

// ...
Run Code Online (Sandbox Code Playgroud)

在您的 services.yaml 上

services:
  email_service:
    class: App\Service\EmailService
    arguments: ['@swiftmailer.mailer.default', '@twig']
Run Code Online (Sandbox Code Playgroud)