我的服务中的渲染视图 [英] RenderView in My service

查看:11
本文介绍了我的服务中的渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 symfony 世界的新手.我想在我的服务中使用渲染,但出现此错误

I'm new of symfony world. I want to use render inside my service, but I got this error

调用未定义的方法 renderView

Call to undefined method renderView

我知道renderView是

I know that renderView is shortcut of

/**
 * 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);
}

但我不知道我必须在我的服务中注入什么.我什至知道使用 php app/console container:debug 命令我可以看到我所有可用的服务,但我不知道如何选择/选择正确的

But I don't know what I have to Injection in my service. I know even that with php app/console container:debug command I Can see all my services available, but I don't know how can take/choose the correct

更新

我尝试添加

arguments: [@mailer,@templating]

但是我得到了 ServiceCircularReferenceException

更新

我用

    arguments: [@service_container]

甚至我的服务

$email = $this->service_container->get('mailer');
$twig = $this->service_container->get('templating');

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

for use service Mail (swift) and render.

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

I don't think that it's best solution. I'd like to injection Only mailer and templating

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

UPDATE After Jason's answer I'm using Symfony 2.3

我的 services.yml

my services.yml

services:
    EmailService:
        class: %EmailService.class%
        arguments:  [@mailer,@templating,%EmailService.adminEmail%]

我收到了这个 ServiceCircularReferenceException

推荐答案

您对 renderView() 的看法是正确的,它只是控制器的快捷方式.当使用服务类并注入模板服务时,您所要做的就是将函数更改为 render().所以代替

You are correct about renderView(), it's only a shortcut for Controllers. When using a service class and inject the templating service, all you have to do is change your function to render() instead. So instead of

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

你会使用

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

来自 Olivia 回复的更新:

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

If you are getting circular reference errors, the only way around them is to inject the whole container. It's not considered best practice but it sometimes cannot be avoided. When I have to resort to this, I still set my class variables in the constructor so I can act as if they were injected directly. So I will do:

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
}

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

Side note, I just tested my own standalone service in Symfony 2.5 and did not receive a circular reference by injecting the mailer and templating services directly.

这篇关于我的服务中的渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆