Symfony 7.3测试环境下如何完全禁用WebProfiler?
在Symfony 7.3测试环境中完全禁用WebProfiler
要解决测试错误返回HTML包含WebProfiler内容的问题,同时避免禁用Bundle时的容器异常,按以下步骤操作:
调整Bundle加载配置
修改config/bundles.php,将WebProfilerBundle的测试环境加载状态设为false:Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => false],清理冲突的配置节点
因为web_profiler.yaml中的when@test区块包含web_profiler配置节点,当Bundle未加载时,容器会找不到对应的扩展导致异常,所以需要:- 直接删除
web_profiler.yaml中的when@test整个区块; - 或者将其中的
framework.profiler配置合并到config/packages/framework.yaml的when@test区块中,确保Profiler完全关闭:when@test: framework: test: true session: storage_factory_id: session.storage.factory.mock_file profiler: enabled: false collect: false
- 直接删除
完成以上修改后,测试环境将完全不加载WebProfilerBundle,错误返回的HTML也不会包含其相关的CSS、JS和内容,错误信息会更清晰易读。
内容的提问来源于stack exchange,提问作者Vincent Decaux
相关产品推荐
相关产品推荐

