Ali*_*ixB 5 dependency-injection symfony twig
我试着注入模板服务,这就是我得到的.
<service id="myproject_notification.service.mail" class="%myproject_notification.service.mail.class%">
<argument type="service" id="mailer" />
<argument type="service" id="templating" />
</service>
Run Code Online (Sandbox Code Playgroud)

如果我从依赖项中评论或删除模板服务,一切都运行良好.我看到了关于这个问题的老问题,但似乎我是目前唯一遇到这个问题的人.难道我做错了什么?
Composer.json
"symfony/symfony": "~2.4",
"twig/extensions": "~1.0",
"symfony/assetic-bundle": "~2.3",
"symfony/swiftmailer-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"sensio/distribution-bundle": "~2.3",
"sensio/framework-extra-bundle": "~3.0",
"sensio/generator-bundle": "~2.3",
"incenteev/composer-parameter-handler": "~2.0",
Run Code Online (Sandbox Code Playgroud)
Composer.lock
"name": "symfony/symfony",
"version": "v2.5.0",
...
"name": "twig/twig",
"version": "v1.15.1",
Run Code Online (Sandbox Code Playgroud)
Sla*_* II 10
您应该始终尽量避免将容器直接注入您的服务.
我认为"循环引用"问题以及可能的性能问题的最佳解决方案是使用从Symfony 2.3开始提供的" Lazy Services "功能.
只需lazy在服务容器配置中标记依赖关系并安装ProxyManager Bridge(查看上面的Lazy Services文档中的详细信息).
我希望有所帮助,欢呼.
此解决方案是解决问题的快速方法.这可以避免,请阅读评论.
对于这种特殊情况,最好将其ServiceContainer注入您的服务中.因为看起来你正在经历一个边缘情况,其中security.context已经注入到某些templating服务(例如帮助程序)中,然后在你的示例中将其间接注入(间接)到security.context.
试试这个:
<service id="myproject_notification.service.mail" class="%myproject_notification.service.mail.class%">
<argument type="service" id="service_container" />
</service>
Run Code Online (Sandbox Code Playgroud)
在类的构造函数中,使用如下:
class YourMailerClass
{
protected $container;
public function __construct(ContainerInterface $container )
{
$this->container = $container;
}
public function sendMail()
{
$mailer = $this->container->get('mailer');
$templating = $this->container->get('templating');
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅Symfony Core开发人员之间关于同一问题的对话:https: //github.com/symfony/symfony/issues/2347
对于大多数情况,由于多种原因,不建议注入服务容器.
| 归档时间: |
|
| 查看次数: |
18602 次 |
| 最近记录: |