php 我的服务中的渲染视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27461260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RenderView in My service
提问by monkeyUser
I'm new of symfony world. I want to use render inside my service, but I got this error
我是 symfony 世界的新手。我想在我的服务中使用渲染,但出现此错误
Call to undefined method renderView
调用未定义的方法 renderView
I know that renderView is shortcut of
我知道 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);
}
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
但我不知道我必须在我的服务中注入什么。我什至知道使用php app/console container:debug
命令我可以看到我所有可用的服务,但我不知道如何采取/选择正确的
update
更新
I tried to add
我试着添加
arguments: [@mailer,@templating]
but I got ServiceCircularReferenceException
但我得到了 ServiceCircularReferenceException
UPDATE
更新
I changed my service.yml with
我改变了我的 service.yml
arguments: [@service_container]
and even my service
甚至我的服务
$email = $this->service_container->get('mailer');
$twig = $this->service_container->get('templating');
for use service Mail (swift) and render.
用于使用服务邮件(swift)和渲染。
I don't think that it's best solution. I'd like to injection Only mailer
and templating
我不认为这是最好的解决方案。我只想注射mailer
并且templating
UPDATE After Jason's answerI'm using Symfony 2.3
更新 Jason 的回答后,我正在使用 Symfony 2.3
my services.yml
我的服务.yml
services:
EmailService:
class: %EmailService.class%
arguments: [@mailer,@templating,%EmailService.adminEmail%]
I got this ServiceCircularReferenceException
我懂了 ServiceCircularReferenceException
回答by Jason Roman
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
你是对的renderView()
,它只是控制器的快捷方式。当使用服务类并注入模板服务时,您所要做的就是将您的函数render()
改为。所以代替
return $this->renderView('Hello/index.html.twig', array('name' => $name));
you would use
你会用
return $this->render('Hello/index.html.twig', array('name' => $name));
Update from Olivia's response:
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
}
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.
旁注,我刚刚在 Symfony 2.5 中测试了我自己的独立服务,并没有通过直接注入邮件程序和模板服务来收到循环引用。
回答by Mateusz
Using constructor dependency injection (tested with Symfony 3.4):
使用构造函数依赖注入(用 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');
// ...
}
}
No need to configure arguments.
无需配置参数。
回答by Dayron Gallardo
This works on Symfony +4.2, assuming your application's namespace is App and your mailer class service is named EmailService.
这适用于 Symfony +4.2,假设您的应用程序的命名空间是 App 并且您的邮件程序类服务名为 EmailService。
On your Service class:
在您的服务类上:
// ...
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');
// ...
}
// ...
On your services.yaml
在您的 services.yaml 上
services:
email_service:
class: App\Service\EmailService
arguments: ['@swiftmailer.mailer.default', '@twig']