Symfony 3.4重写FOSUserBundle注册控制器时自动注入服务失败
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:
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.factoryEnsure 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']Double-check your controller code
Your existingRegistrationControllersetup 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; } }Confirm your bundle inheritance
YourAppBundle.phpis 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

