You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

服务器端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-bundle 6.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ń

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 21:30:56