所以,我正在试图弄清楚这些听众,但我在symfony网站上找到关于他们的任何信息时遇到了问题.
最初,我想创建一个会在每个页面加载时触发的侦听器...我认为这可能对整个系统性能有害,所以我想它只触发:/和/ otherpage
但同样,我在找到有关如何开始使用监听器的任何信息时遇到了问题.任何帮助表示赞赏..所有这些听众都会这样做,正在使用Doctrine来检查数据库并根据它找到的内容设置会话.
再次,任何帮助或建议表示赞赏.谢谢.
gre*_*reg 10
我做类似的事情检查子域名没有改变.您可以将侦听器作为服务放入配置文件中,如下所示:
services:
    page_load_listener:
        class: Acme\SecurityBundle\Controller\PageLoadListener
        arguments: 
            security: "@security.context", 
            container: "@service_container"
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 64 }
我不确定优先级是如何工作的,但我发现如果它设置得太高,它将无法在应用程序的其余部分之前运行.这是我的待办事项列表,可以进行更多研究.
以下是侦听器外观的示例.
namespace Acme\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class PageLoadListener extends controller
{
    private $securityContext;
    protected $container;
    protected $query;
    public function __construct(SecurityContext $context, $container, array $query = array())
    {
        $this->securityContext = $context;
        $this->container = $container;
        $this->query = $query;
    }
    public function onKernelRequest(GetResponseEvent $event)
    {       
        //if you are passing through any data
        $request = $event->getRequest();
        //if you need to update the session data
        $session = $request->getSession();              
        //Whatever else you need to do...
    }
}
我不确定将其设置为仅在某些页面上运行的最佳方式,但我最好的猜测是检查路由并且只在路由匹配您设置的任何内容时才点击它.
希望能让你入门!
格雷格
| 归档时间: | 
 | 
| 查看次数: | 9148 次 | 
| 最近记录: |