Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Overriding Controllers

I use the FOSUserBundle and I want to override his registerAction controller. I read the documentation related to overriding controllers of FOSUserBundle but it doesn't work. By echoing a little message in the controller, it is not print in the template.

Here is the way 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 in the file named RegistrationController.php in the controller directory of my bundle :

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;
    }
}

Have you got any solution ?

like image 617
Jeffrey Muller Avatar asked Mar 24 '26 19:03

Jeffrey Muller


1 Answers

Just spent hours trying to get this working and finally figured it out. The part I was missing was extending my User class with a User entity from within MyUserBundle. For instance:

namespace MyNamespace\MyMainBundle\Entity;

use MyNamespace\MyUserBundle\Entity\User as BaseUser;

class User extends BaseUser
{
}

The User entity is identical to the one in the FOSUserBundle (just with a different namespace).

namespace MyNamespace\MyUserBundle\Entity;

use FOS\UserBundle\Model\User as AbstractUser;

abstract class User extends AbstractUser
{
}

If I didn't do this, as Jeffrey mentioned, MyUserBundle wasn't used at all (as if it didn't exist). Now all my overridden views, controllers etc. are being used. Hope this helps.

like image 193
Dally Horton Avatar answered Mar 27 '26 19:03

Dally Horton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!