【发布时间】:2012-02-08 01:29:24
【问题描述】:
我们需要在监听器中访问数据库信息。 我们在 service.yml 中配置监听器 听者是这样的:
namespace company\MyBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class RequestListener
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
...
我们如何在 onKernelRequest 函数中访问学说?
我尝试从控制器扩展并执行以下操作:
$em = $this->getDoctrine()->getEntityManager();
它有效,但我认为这是一种不好的做法。
【问题讨论】:
-
感谢所有 cmets。都是不错的选择。
标签: symfony doctrine-orm listener bundle