Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd a default role during user registration with FOSUserBundle
    text
    copied!<p>Version : Symfony 2.2</p> <p>I'm trying to add a default role when a user register on my website. I use FOSUserBundle and i see that when a user register the role field is empty in a database. I begin with this huge bundle and it's not very easy to understand. So i read all the documentation and i'm not sur what to do.</p> <p>For now, i create an Event to add this role dynamically, but it doesn't work (i have no error but my database is still empty) I'm not even sur this is the good way to do that ?</p> <p>My Event :</p> <pre><code>use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Event\FormEvent; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\DependencyInjection\ContainerInterface; class AddDefaultRoleListener implements EventSubscriberInterface { private $container; public function __construct(Container $container) { $this-&gt;container = $container; } /** * {@inheritDoc} */ public static function getSubscribedEvents() { return array( FOSUserEvents::REGISTRATION_SUCCESS =&gt; 'onAddDefaultRoleSuccess', ); } public function onAddDefaultRoleSuccess(FormEvent $event) { $doctrine = $this-&gt;container-&gt;get('doctrine'); $em = $doctrine-&gt;getManager(); $user = $event-&gt;getForm()-&gt;getData(); $user-&gt;addRole('ROLE_USER'); //$user-&gt;setRoles(array('ROLE_USER')); $em-&gt;persist($user); } } </code></pre> <p>As you see i create a simple event which listen on REGISTRATION_SUCCESS, but nothing seems to work. It's my first try with Events and services. So if someone has an advice, i'll take it :)</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload