3
votes

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 ?

2
you won't see it that way, you need to add exit or die, i.e. echo 'foo';exit; - Inoryy
It doesn't work, because Symfony calls the registerAction method of FOSUserBundle, not mine. So, no echo(), nor exit(). - Jeffrey Muller
Delete your own bundle's routing.yml (which is blank by default). I was using the default and Symfony really will read everything from the 'child' bundle in place of the parent if it has the same filename. - Tatsh

2 Answers

3
votes

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.

1
votes

It doesnt work for me. i try to do your solution. Finally i just clear the cache runnig the php bin/console cache:clear inside of my proyect of course. And func like a charm.

So.. try to clear the cache.

And sorry for my bad english.