首页 > 解决方案 > Symfony 事件 - 如何不通过 Profiler 或预热触发内核事件

问题描述

Symfony v3.4 项目,具有多个已定义的侦听器/订阅者,用于各种目的。他们kernel.requestkernel.controller事件有关。

我注意到所有这些也会被 Symfony 的分析器工具发出的任何请求触发,以及在 Symfony 正在预热代码缓存的情况下。

真的应该这样吗?当页面请求来自 Profiler 时,是否有一种很好的方法可以不触发定义的 kernel.* 事件(至少是特定事件)?最好的方法是什么?

关于在热身时排除这些事件 - 我相信如果它不会做所有的连接等,热身会更快,所以它的发展会稍微快一些。

我在这里看到了一个较早的自我回答问题:如何防止 Symfony 分析器访问或执行侦听器,但在我的用例中,这似乎需要做太多工作 - 更改系统中的所有现有控制器。此外,其他提供的评论/补丁似乎并不是处理这个问题的好方法。感谢您的任何见解。

标签: symfonyeventscontrollerprofiler

解决方案


不幸的是,这是不可能的,因为它WebProfilerBundle依赖于symfony 的事件系统

由于探查器路由以_前缀开头,您可以通过以下方式忽略它:

$route = $event->getRequest()->attributes->get('_route');
if (strpos($route, '_') === 0) {
    return;
}
  _twig_error_test              ANY      ANY      ANY    /_error/{code}.{_format}           
  _wdt                          ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home                ANY      ANY      ANY    /_profiler/                        
  _profiler_search              ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar          ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_phpinfo             ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results      ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler_open_file           ANY      ANY      ANY    /_profiler/open                    
  _profiler                     ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router              ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception           ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css       ANY      ANY      ANY    /_profiler/{token}/exception.css 

推荐阅读