Reputation: 114
I have a user entity with firstname,lastname and url attributes, extending the FOSUserBundle User. I want to store in the url attribute the value of fistname and lastname as one strig.
Some people suggested that i should use a listener and a service, so i 've made:
The event seems to work after registration, but i can't find a way to change my user url...
servises.yml
services:
kernel.listener.RegistrationListener:
class: Apana\Bundle\MainBundle\EventListener\RegistrationListener
tags:
- { name: kernel.event_listener, event: fos_user.registration.completed, method: onUserRegistration }
And in RegistrationListener.php :
<?php
namespace Apana\Bundle\MainBundle\EventListener;
use Apana\Bundle\MainBundle\Entity\User;
use Symfony\Component\Security\Core\SecurityContext;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
class RegistrationListener
{
public function onUserRegistration()
{
$user = new User();
$ap= = $user->getFirstname();
echo($ap);exit();
}
}
Upvotes: 0
Views: 1860
Reputation: 114
Ok, i 've solved my problem with an eventlistener and the FOSUserBundle UserEvent
<?php
namespace Apana\Bundle\MainBundle\EventListener;
use FOS\UserBundle\Event\UserEvent;
class RegistrationListener
{
public function onUserRegistration(UserEvent $event)
{
$user = $event->getUser();
$url = strtolower($user->getFirstname().".".$user->getLastname());
$user->setUrl($url);
}
}
Upvotes: 0
Reputation: 2839
If I understand correctly, what you want to do is a slug with the firstname and lastname field.
First and last name to be used as slug must also be sanitizated to replace accented characters, spaces, etc..
To do everything automatically, you can use the Doctrine Extension, in particular Sluggable: https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sluggable.md
This is an example using annotations for what you need:
<?php
// src/Acme/UserBundle/Entity/User.php
namespace Acme\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(length=64, unique=false)
*/
private firstname;
/**
* @ORM\Column(length=64, unique=false)
*/
private lastname;
/**
* @Gedmo\Slug(fields={"firstname", "lastname"}, separator="-")
* @ORM\Column(length=128, unique=true)
*/
private $slug;
public function __construct()
{
parent::__construct();
// your own logic
}
//... setter and getter...
}
Upvotes: 1
Reputation: 18660
Why you don't use UserManager
for that instead of a listener? I would override the FOSUser register with my own and do something like:
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->createUser();
$user->setUrl("any data");
It's more easy and less complicated at least for me
Upvotes: 1