服务器端Symfony自动注入失败问题(本地运行正常)
Symfony Autowire 错误排查:AuthenticationUtils 服务不存在
错误信息
Cannot autowire argument $authenticationUtils of "App\Controller\LoginController::index()": it references class "Symfony\Component\Security\Http\Authentication\AuthenticationUtils" but no such service exists.
已知情况
- 已安装
symfony/security-bundle6.0.*,composer.json配置完整 - vendor目录下
/vendor/symfony/security-http/Authentication/AuthenticationUtils.php文件存在
排查方案
检查Security Bundle注册状态
打开config/bundles.php,确认以下行存在且未被注释:Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],清理服务器缓存
服务器端执行缓存清理命令(生产环境):php bin/console cache:clear --env=prod开发环境可去掉
--env=prod参数。确认Security配置启用HTTP认证
编辑config/packages/security.yaml,确保防火墙配置中包含form_login或其他依赖AuthenticationUtils的认证方式,示例:security: firewalls: main: form_login: login_path: app_login check_path: app_login若缺少相关认证配置,SecurityBundle不会自动注册AuthenticationUtils服务。
重新生成自动加载映射
执行命令优化自动加载:composer dump-autoload --optimize生产环境可重新安装依赖并优化:
composer install --no-dev --optimize-autoloader验证依赖版本一致性
检查security-bundle和security-http版本是否匹配:composer show symfony/security-bundle composer show symfony/security-http确保两者均为6.0.*版本,避免版本不兼容导致的服务注册异常。
内容的提问来源于stack exchange,提问作者Jerzy Stępień
相关产品推荐
相关产品推荐

