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

Symfony 3.4重写FOSUserBundle注册控制器时自动注入服务失败

Fixing Autowire Error When Extending FOSUserBundle's RegistrationController in Symfony 3.4

The error you're hitting occurs because Symfony's autowiring can't automatically map the FactoryInterface dependency required by the parent RegistrationController. To fix this, we just need to explicitly alias that interface to the correct FOSUserBundle service.

Here's a step-by-step solution:

  1. Update your services configuration (typically in app/config/services.yml)
    Add an alias that tells Symfony which concrete service to use for the interface:

    services:
        # ... keep your existing service definitions
    
        # Map the FactoryInterface to the registration form factory service
        FOS\UserBundle\Form\Factory\FactoryInterface:
            alias: fos_user.registration.form.factory
    
  2. Ensure autowiring is enabled for your controller
    If you haven't already, enable autowiring for your AppBundle controllers to let Symfony handle dependency injection correctly:

    services:
        AppBundle\Controller\:
            resource: '../../src/AppBundle/Controller'
            autowire: true
            public: true
            tags: ['controller.service_arguments']
    
  3. Double-check your controller code
    Your existing RegistrationController setup looks correct, but let's confirm it's properly structured:

    // src/AppBundle/Controller/RegistrationController.php
    namespace AppBundle\Controller;
    
    use FOS\UserBundle\Controller\RegistrationController as BaseController;
    use Symfony\Component\HttpFoundation\Request;
    
    class RegistrationController extends BaseController
    {
        public function registerAction(Request $request)
        {
            $response = parent::registerAction($request);
            // Add your custom logic here (like post-registration actions)
            return $response;
        }
    }
    
  4. Confirm your bundle inheritance
    Your AppBundle.php is correctly set to inherit from FOSUserBundle, so that part is good to go:

    // src/AppBundle/AppBundle.php
    <?php
    namespace AppBundle;
    
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    
    class AppBundle extends Bundle
    {
        public function getParent()
        {
            return 'FOSUserBundle';
        }
    }
    

Finally, clear your Symfony cache to apply all the configuration changes:

php bin/console cache:clear

This should resolve the autowire error by explicitly telling Symfony which service to inject when the FactoryInterface is requested.

内容的提问来源于stack exchange,提问作者Marco Koopman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:45:49