我正在使用FOSuserbundle,我正在尝试在注册时预先填充数据库中的字段。 我正在尝试自动添加“http://localhost:1337/digitalartlab/web/profile/ {username} / checkin”等值。用户名将是此字符串的唯一变量。我怎么能这样做?
user.php的
/**
* Set checkinurl
*
* @param string $checkinurl
*
* @return User
*/
public function setCheckinurl($checkinurl)
{
$this->checkinurl = $checkinurl;
return $this;
}
/**
* Get checkinurl
*
* @return string
*/
public function getCheckinurl()
{
return $this->checkinurl;
}
registrationtype.php(我在注册表中添加了一些额外的字段)。
<?php
// src/AppBundle/Form/RegistrationType.php
namespace DigitalArtLabBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstname');
$builder->add('lastname');
$builder->add('address');
$builder->add('zipcode');
$builder->add('Expertises');
$builder->add('Interesses');
$builder->add('saldo', 'integer', array(
'label' => 'Saldo',
'data' => '0'
));
}
public function getParent()
{
return 'fos_user_registration';
}
public function getName()
{
return 'app_user_registration';
}
}
registrationcontroller :(禁用的代码是我自己尝试填充字段)
public function registerAction()
{
$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
$authUser = false;
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_user_registration_check_email';
} else {
$authUser = true;
/*$userdata = $this->get('fos_user.user_manager')->updateUser($user->getUsername());
$namespace = 'localhost:1337/digitalartlab/web/profile';
$userdata->setCheckinurl('http://'.$namespace.'/'.$user->getUsername().'/checkin');*/
$route = 'fos_user_registration_confirmed';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$url = $this->container->get('router')->generate($route);
$response = new RedirectResponse($url);
if ($authUser) {
$this->authenticateUser($user, $response);
}
return $response;
}
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
}
我已经阅读了文档,但这对我来说真的很混乱。我希望有人能帮助我说明这是如何运作的。感谢。
答案 0 :(得分:1)
您可以创建注册侦听器来执行此任务。请参阅文档here。
请务必在services.yml
:
services:
your_bundle.registration_listener:
class: YourBundle\EventListener\RegistrationListener
tags:
- { name: kernel.event_subscriber }
监听器:
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* RegistrationListener
*
*/
class RegistrationListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
);
}
public function onRegistrationSuccess(FormEvent $event)
{
$user = $event->getForm()->getData();
$userName = $user->getUserName();
$checkInUrl = 'http://localhost:1337/digitalartlab/web/profile/' . $username;
$user->setCheckinurl($checkInUrl);
}
}