Symfony2 Overriding Controllers
I am using FOSUserBundle and I want to override its registerAction controller. I have read the documentation related to FOSUserBundle overriding controllers, but it doesn't work. By repeating the little message in the controller, it is not printed in the template.
This is how I chose:
I inherit my bundle from FOSUserBundle:
namespace Jheberg\MembersBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class JhebergMembersBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
And I override registerAction on a file named RegistrationController.php
in my package controller directory:
namespace Jheberg\MembersBundle\Controller;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
class RegistrationController extends BaseController
{
public function registerAction()
{
echo 'foo';
$response = parent::registerAction();
// do custom stuff
return $response;
}
}
Do you have a solution?
source to share
Just spent hours trying to get this to work and finally figured it out. The part I was missing was extending my User class with the User object from MyUserBundle. For example:
namespace MyNamespace\MyMainBundle\Entity;
use MyNamespace\MyUserBundle\Entity\User as BaseUser;
class User extends BaseUser
{
}
The custom object is identical to the one in the FOSUserBundle (only with a different namespace).
namespace MyNamespace\MyUserBundle\Entity;
use FOS\UserBundle\Model\User as AbstractUser;
abstract class User extends AbstractUser
{
}
If I hadn't, as Jeffrey pointed out, MyUserBundle would not have been used at all (as if it wasn't there). Now all my overridden views, controllers, etc. Are used. Hope this helps.
source to share