FriendsOfSymfony / FOSUserBundle

Provides user management for your Symfony project. Compatible with Doctrine ORM & ODM, and custom storages.

Home Page:https://symfony.com/doc/master/bundles/FOSUserBundle/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Override controllers

victor-paumier opened this issue · comments

Symfony FOSUserBundle versions:
v2.1.2

Description of the problem including expected versus actual behavior:
Hello,

When overriding controllers, I often need to use same properties than the default one.
But as they are all declared as private I have to set them again in order to access them in my controller. See the example below for the RegistrationController.

private $eventDispatcher;
private $formFactory;
private $userManager;
private $tokenStorage;

public function __construct(
    EventDispatcherInterface $eventDispatcher,
    FactoryInterface $formFactory,
    UserManagerInterface $userManager,
    TokenStorageInterface $tokenStorage
) {
    parent::__construct($eventDispatcher, $formFactory, $userManager, $tokenStorage);
    $this->eventDispatcher = $eventDispatcher;
    $this->formFactory = $formFactory;
    $this->userManager = $userManager;
    $this->tokenStorage = $tokenStorage;
}

So my question is quite simple, is there a real reason to declare these properties as private, or is it possible to change them for protected to avoid this redeclaration?

Thank you in advance :)

Making them protected would mean that all these properties would then have to be covered by the BC policy. This makes maintenance much harder (protected properties are even worse than protected methods in regards of writing a BC layer for a change around them).

If you want to override the behavior of some routes, my suggestion is to avoid extending the FOSUserBundle class. Override the route for which you want to make a change, and write your own controller there (potentially injecting the FOSUserBundle controller in case you wanted to wrap the original method).
Or even better, write your logic in listeners on the FOSUserBundle events if they are enough for you and don't override the controller at all (that's why we have a bunch of events)

@stof Thanks for the quick reply.

Indeed seems legit, thank you for the explanation.
I will follow your recommandations and use events as much as I can then!