我正在使用symfony 2.4,我需要在每个请求上查询数据库,以便为应用程序提供一些配置值,我该怎么办?
答案 0 :(得分:0)
创建监听器:
// src/Acme/DemoBundle/EventListener/AcmeListener.php
namespace Acme\DemoBundle\EventListener;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AcmeListener
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->container->get('doctrine.orm.manager')->getRepository('repositoryName'); // load data from database
}
}
定义监听器:
# app/config/config.yml
services:
kernel.listener.your_listener_name:
class: Acme\DemoBundle\EventListener\AcmeListener
arguments: ["@service_container"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
阅读this示例
也许有用