Symfony添加Bundle路由报错:Bundle不存在或未启用,已激活Bundle
报错信息
Bundle "bundlename" does not exist or it is not enabled. Maybe you forgot to add it in the "registerBundles()" method of your "App\Kernel.php" file?
问题场景
已在config/bundles.php中激活目标Bundle,但在项目config/routes.yaml中配置加载Bundle路由时触发上述报错。Bundle内包含routes.yaml,项目路由配置如下:
app_crop: # loads routes from the given routing file stored in some bundle resource: '@bundlename/UploadImagesBundle/Resources/config/routes.yaml'
解决方案
修正Bundle别名引用
Symfony中@BundleAlias对应的是Bundle主类的别名(通常是Bundle名称,如UploadImagesBundle),而非命名空间前缀。打开config/bundles.php查看你的Bundle注册项:return [ // ... bundlename\UploadImagesBundle\UploadImagesBundle::class => ['all' => true], ];项目路由配置里的
@bundlename要改成@UploadImagesBundle,正确的路由配置应为:app_crop: resource: '@UploadImagesBundle/Resources/config/routes.yaml'修复Bundle内路由文件的缩进错误
YAML对缩进要求严格,你提供的Bundle内routes.yaml存在缩进问题,修正后应为:route_name: path: /crop controller: bundlename\UploadImagesBundle\Controller\Crop\CropController::crop options: expose: true验证Bundle注册状态
执行命令bin/console debug:bundles,检查目标Bundle是否出现在列表中且状态为Enabled。如果未显示,核对bundles.php中的类名、命名空间是否与实际文件路径一致。清除缓存
执行bin/console cache:clear清除缓存,确保所有配置变更生效。
内容的提问来源于stack exchange,提问作者ViceStar

