Question

1) How can I add new fields in table and in registration action (show new fields on reg page)? For example: I want to add new fields last_name, age.

2) I added new listener for REGISTRATION_COMPLETED

/src/Acme/UserBundle/EventListener/RegistrationCompletedListener.php:

<?php
namespace Acme\UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Listener
 */
class RegistrationCompletedListener implements EventSubscriberInterface
{
    private $router;

    public function __construct(UrlGeneratorInterface $router)
    {
        $this->router = $router;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompletedSuccess',
        );
    }

    public function onRegistrationCompletedSuccess(FormEvent $event)
    {
        $url = $this->router->generate('homepage');

        $event->setResponse(new RedirectResponse($url));
    }
}

/src/Acme/UserBundle/Resources/config/services.yml:

services:
    acme_user.registration_completed:
        class: Acme\UserBundle\EventListener\RegistrationCompletedListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

Why don't work?

Was it helpful?

Solution

1) You should extend Base User class and add there your new fields, like this:

namespace Your\CustomBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{    
    /**
     * @var string
     *
     * @ORM\Column(name="first_name", type="string", length=255)
     */
    private $firstName;

    /**
     * @var string
     *
     * @ORM\Column(name="last_name", type="string", length=255)
     */
    private $lastName;

}

And update appconfig/config.yml:

#FOSLUserBundle Configuration
fos_user:
    user_class: Your\CustomBundle\Entity

Then you need extend and configure new registration form. Here is a link how you can do that.

[Edit]:

2)

Create event listener like this:

namespace Your\Bundle\EventListener;

use FOS\UserBundle\Event\FilterUserResponseEvent;

class UserListener
{

    public function onRegistrationCompleted(FilterUserResponseEvent $event){

        $user = $event->getUser();
        //do sth....

    }
}

And register service for that:

services:
    some_name.security.registration_listener:
        class: Your\Bundle\EventListener\UserListener
        tags:
            - { name: kernel.event_listener, event: fos_user.registration.completed, method: onRegistrationCompleted }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top