执行cache:clear脚本时Symfony 7出现配置错误求助
问题重现
使用Symfony CLI创建7.0版本webapp项目:
symfony new my_project_directory --version="7.0.*" --webapp
进入目录执行symfony server:start时,触发cache:clear脚本失败,错误提示:
Executing script cache:clear [KO]
[KO]
Script cache:clear returned with error code 1
!!
!! In SecurityExtension.php line 94:
!!
!! Enabling bundle "Symfony\Bundle\SecurityBundle\SecurityBundle" and not conf
!! iguring it is not allowed.
!!
!!
!!
Script @auto-scripts was called via post-update-cmd
解决步骤
检查安全配置文件是否存在
确认项目根目录下的config/packages/security.yaml文件是否存在。Symfony的--webapp脚手架应自动生成该文件,但可能因网络或CLI异常导致文件缺失。手动创建/修复security.yaml配置
若文件不存在,新建config/packages/security.yaml并填入基础安全配置:security: password_hashers: Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' providers: app_user_provider: entity: class: App\Entity\User property: email firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: lazy: true provider: app_user_provider access_control: # 可根据需求添加权限控制规则,示例: # - { path: ^/admin, roles: ROLE_ADMIN }若文件已存在但内容为空或不完整,补充上述基础配置即可。
重新安装webapp脚手架(可选)
若手动配置后仍有问题,执行以下命令重新安装webapp所需的依赖和配置:composer require webapp验证SecurityBundle注册
检查config/bundles.php中是否包含SecurityBundle的注册项,确保存在以下行:Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
完成上述步骤后,重新执行symfony server:start,cache:clear脚本即可正常运行。
内容的提问来源于stack exchange,提问作者Mathieu Le Ray

