问题描述
我有一个要与用户会话关联的实体.我创建了一项服务,以便我可以从任何地方获取这些信息.
I've got an Entity that I want to associate with the users session.I created a service so that I could reach this info from where ever.
在服务中,我将实体 ID 保存在会话变量中在 getEntity()
方法中,我得到了会话变量,并使用学说找到实体并返回它.
in the service i save the entities id in an session variableand in the getEntity()
method i get the session variable and with doctrine find the entity and return it.
这样到模板我应该可以调用 {{ myservice.myentity.myproperty }}
this way to the template i should be able to call {{ myservice.myentity.myproperty }}
问题是到处都在使用myservice,我不想每次都在Action中获取它并将其附加到视图数组中.
The problem is that myservice is used all over the place, and I don't want to have to get it in every since Action and append it to the view array.
有没有办法让服务从所有视图都可以访问,比如会话 {{ app.session }}
?
Is there a way to make a service accessible from all views like the session {{ app.session }}
?
推荐答案
解决方案
通过创建自定义服务,我可以从任何地方使用
The solution
By creating a custom service i can get to that from where ever by using
$this->get('myservice');
这一切都由http://symfony.com/doc/current/book/service_container.html
但我会给你一些演示代码.
But I'll give you some demo code.
第一个片段是实际的服务
This first snippet is the actual service
<?php
namespace MyBundle\AppBundle\Extensions;
use Symfony\Component\HttpFoundation\Session;
use Doctrine\ORM\EntityManager;
use MyBundle\AppBundle\Entity\Patient;
class AppState
{
protected $session;
protected $em;
function __construct(Session $session, EntityManager $em)
{
$this->session = $session;
$this->em = $em;
}
public function getPatient()
{
$id = $this->session->get('patient');
return isset($id) ? $em->getRepository('MyBundleStoreBundle:Patient')->find($id) : null;
}
}
在你的config.yml
中注册它
services:
appstate:
class: MyBundle\AppBundle\Extensions\AppState
arguments: [@session, @doctrine.orm.entity_manager]
现在我们可以像我之前所说的那样,在我们的控制器中使用
Now we can as I said before, get the service in our controllers with
$this->get('myservice');
但由于这是一项全球服务,我不想在每个控制器和每个动作中都这样做
But since this is a global service I didn't want to have to do this in every controller and every action
public function myAction()
{
$appstate = $this->get('appstate');
return array(
'appstate' => $appstate
);
}
所以现在我们去创建一个 Twig_Extension
so now we go create a Twig_Extension
<?php
namespace MyBundle\AppBundle\Extensions;
use MyBundle\AppBundle\Extensions\AppState;
class AppStateExtension extends \Twig_Extension
{
protected $appState;
function __construct(AppState $appState) {
$this->appState = $appState;
}
public function getGlobals() {
return array(
'appstate' => $this->appState
);
}
public function getName()
{
return 'appstate';
}
}
通过使用依赖注入,我们现在拥有在名为 appstate 的 twig 扩展中创建的 AppState 服务
By using dependency injection we now have the AppState Service that we created in the twig extension named appstate
现在我们用 symfony 注册(再次在配置文件内的 services
部分)
Now we register that with the symfony (again inside the services
section inside the config-file)
twig.extension.appstate:
class: MyBundle\AppBundle\Extensions\AppStateExtension
arguments: [@appstate]
tags:
- { name: twig.extension }
重要的部分是标签",因为这是 symfony 用来查找所有树枝扩展的东西
The important part being the "tags", since this is what symfony uses to find all twig extensions
我们现在设置为通过变量名称在我们的树枝模板中使用我们的 appstate
We are now set to use our appstate in our twig templates by the variable name
{{ appstate.patient }}
或
{{ appstate.getPatient() }}
太棒了!
这篇关于Symfony 2.0.3 全局模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!