Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The recommended way to do it as indicated by a main contributor to the FOSUserBundle (<a href="https://github.com/FriendsOfSymfony/FOSUserBundle/issues/928#issuecomment-11840801" rel="nofollow noreferrer">in the comment here linked</a>) is to register an Event Listener on the REGISTRATION_SUCCESS event and use the <code>$event-&gt;getForm()-&gt;getData()</code> to access the user and modify it. Following those guidelines, I created the following listener (which works!):</p> <pre><code>&lt;?php // src/Acme/DemoBundle/EventListener/RegistrationListener.php namespace Acme\DemoBundle\EventListener; use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Event\FormEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Listener responsible for adding the default user role at registration */ class RegistrationListener implements EventSubscriberInterface { public static function getSubscribedEvents() { return array( FOSUserEvents::REGISTRATION_SUCCESS =&gt; 'onRegistrationSuccess', ); } public function onRegistrationSuccess(FormEvent $event) { $rolesArr = array('ROLE_USER'); /** @var $user \FOS\UserBundle\Model\UserInterface */ $user = $event-&gt;getForm()-&gt;getData(); $user-&gt;setRoles($rolesArr); } } </code></pre> <p>Also, the service needs to be registered as follows:</p> <pre><code>// src/Acme/DemoBundle/Resources/config/services.yml services: demo_user.registration_listener: class: Acme\DemoBundle\EventListener\RegistrationListener arguments: [] tags: - { name: kernel.event_subscriber } </code></pre> <p>Notice that adding a default role in the User class __construct() may have some issues as indicated in <a href="https://stackoverflow.com/a/12403785/1362009">this other answer</a>.</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