Api Platform模块Symfony自动配置仓库命名空间问题
问题核心
执行cache:clear时Symfony自动配置报错,原因是使用通配符导入../vendor/controleonline/*/src/*资源时,错误将src目录解析为命名空间的一部分,导致类名被错误解析为ControleOnline\common\src\Listener\LogListener,而实际类的命名空间应为ControleOnline\Common\Listener(src是源码目录,不属于命名空间范畴)。
报错信息
Executing script cache:clear [KO] [KO] Script cache:clear returned with error code 1 !! !! In FileLoader.php line 174: !! !! Expected to find class "ControleOnline\common\src\Listener\LogListener" in !! file "/home/control2/sistemas/controleonline/api/vendor/controleonline/comm !! on/src/Listener/LogListener.php" while importing services from resource ".. !! /vendor/controleonline/*/src/*", but it was not found! Check the namespace !! prefix used with the resource in /home/control2/sistemas/controleonline/api !! /config/services.yaml (which is loaded in resource "/home/control2/sistemas !! /controleonline/api/config/services.yaml"). !! !! !! In FileLoader.php line 224: !! !! Expected to find class "ControleOnline\common\src\Listener\LogListener" in !! file "/home/control2/sistemas/controleonline/api/vendor/controleonline/comm !! on/src/Listener/LogListener.php" while importing services from resource ".. !! /vendor/controleonline/*/src/*", but it was not found! Check the namespace !! prefix used with the resource. !! !! !! Script @auto-scripts was called via post-update-cmd
解决方案
针对config/services.yaml中的自动配置错误,有两种修复方式:
方式一:为每个模块单独配置(推荐,更清晰)
明确每个模块的命名空间与源码路径的对应关系,避免通配符解析错误:
services: # 配置Common模块 ControleOnline\Common\: resource: '../vendor/controleonline/common/src/*' exclude: '../vendor/controleonline/common/src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}' # 配置Products模块 ControleOnline\Product\: resource: '../vendor/controleonline/api-platform-products/src/*' exclude: '../vendor/controleonline/api-platform-products/src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
方式二:调整通配符配置,指定子目录
使用subdirectory选项告知Symfony,src是每个模块的子目录而非命名空间的一部分:
services: ControleOnline\: resource: '../vendor/controleonline/*/' subdirectory: 'src' exclude: '{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
验证步骤
- 执行命令验证服务是否能被正确识别:
php bin/console debug:container ControleOnline\Common\Listener\LogListener
- 重新清理缓存,确认报错消失:
php bin/console cache:clear
内容的提问来源于stack exchange,提问作者Luiz Kim Dias Ferreira
相关产品推荐
相关产品推荐

