Search code examples
symfonyfosuserbundlesymfony-3.4

How to override FOSUserBundle's ProfileController in Symfony 3?


I'm trying to override some parts of the FOSUserBundle.

I have to custom the ProfileController (especially the showAction). I looked for a solution, and found that I had to create a new ProfileController in my UserBundle, and inherits the original FOSUserBundle ProfileController.

That's what I have done.

use FOS\UserBundle\Controller\ProfileController as BaseController;

class ProfileController extends BaseController

I also know how to override twig views by creating same name files in my own UserBundle.

My problem is, that I don't know how to make Symfony using my ProfileController instead of the original.

Do I have to change routes in App/Config/routing.yml ?

Or simply create an .xml routing file in my UserBundle, and then import it in App/Config/routing.yml?

I first made the mistake of customing FOSUserBundle files but I know it wasn't the right way, that's why I'm trying to do clean changes now.


Solution

  • The ProfileController is registered as a service named fos_user.profile.controller as you can see in this configuration file.

    In order to override the controller/service (for Symfony 3.4) you need to re-define this service inside your app/config/services.yml:

    services:
      # [..]
      'fos_user.profile.controller':
        class: 'Your\Namespace\ProfileController'
        public: true
        arguments:
          - '@event_dispatcher'
          - '@fos_user.profile.form.factory'
          - '@fos_user.user_manager'
        calls:
          - [ 'setContainer', [ '@service_container' ]]
    

    Now clear your cache. Symfony will then use your ProfileController class as the service named fos_user.profile.controller.