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

Symfony3.4+FOSBundle密码修改路由报错:指定服务为私有弃用提示

Fixing "fos_user.change_password.form.factory" is Private Deprecation in Symfony 3.4 + FOSUserBundle

Hey there, I’ve helped several developers troubleshoot this exact deprecation warning, so let’s break down what’s happening and how to fix it properly.

The error User Deprecated: The "fos_user.change_password.form.factory" service is private stems from Symfony 3.4’s shift toward making most services private by default — a best practice to enforce dependency injection instead of direct container calls. FOSUserBundle might either be outdated, or your custom code is directly fetching this private service from the container.

Solutions

If you’re directly calling $this->get('fos_user.change_password.form.factory') in your controllers or services, you need to replace that with constructor injection. This aligns with Symfony’s best practices and avoids the deprecation.

Example in a Controller:

First, inject the FactoryInterface (the official interface for the form factory) via your controller’s constructor:

use FOS\UserBundle\Form\Factory\FactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class CustomChangePasswordController extends Controller
{
    private $changePasswordFormFactory;

    public function __construct(FactoryInterface $changePasswordFormFactory)
    {
        $this->changePasswordFormFactory = $changePasswordFormFactory;
    }

    public function changePasswordAction(Request $request)
    {
        // Use the injected factory instead of fetching from the container
        $form = $this->changePasswordFormFactory->createForm();
        
        // Rest of your password change logic...
    }
}

Enable Autowiring (Simpler Option):

If you have autowiring enabled in your app/config/services.yml, Symfony will automatically inject the service without extra configuration. Make sure your controller is registered as a service (Symfony 3.4 auto-registers controllers in src/Controller if autowiring is on):

# app/config/services.yml
services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    AppBundle\Controller\:
        resource: '../../src/AppBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

2. Upgrade FOSUserBundle to a Symfony 3.4-Compatible Version

If the deprecation is coming from FOSUserBundle’s internal code (not your custom code), you’re likely using an outdated version of the bundle. FOSUserBundle 2.1.x and above are fully compatible with Symfony 3.4.

Update your composer.json to require the correct version:

"friendsofsymfony/user-bundle": "^2.1"

Then run the update command:

composer update friendsofsymfony/user-bundle

If you need a quick fix and can’t adjust code or upgrade the bundle right now, you can force the service to be public in your services.yml. However, this goes against Symfony’s service design principles and will cause issues if you upgrade to Symfony 4+ later.

Add this to app/config/services.yml:

services:
    fos_user.change_password.form.factory:
        public: true

Stick with the first two solutions for a sustainable fix — dependency injection is the way to go for long-term maintainability, and keeping your bundles updated ensures compatibility with Symfony’s evolving features.

内容的提问来源于stack exchange,提问作者Muhammad Shaaban

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:07:58