php - Error in redirection of user after REGISTRATION_SUCCESS event in FOSUserBundle -
i trying redirect user form before user confirmation email. after submit of register form want redirect user form fill can assign roles user. after submission of form user should confirmation email. after confirmation user login automatically.
full code user management fub
registrationsuccesslistener.php
namespace usr\userbundle\eventlistener; use fos\userbundle\fosuserevents; use fos\userbundle\event\getresponseuserevent; use symfony\component\eventdispatcher\eventsubscriberinterface; use symfony\component\httpfoundation\redirectresponse; use symfony\component\routing\generator\urlgeneratorinterface; class registrationsuccesslistener implements eventsubscriberinterface { private $router; public function __construct(urlgeneratorinterface $router) { $this->router = $router; } /** * {@inheritdoc} */ public static function getsubscribedevents() { return array( fosuserevents::registration_success => 'onregistrationsuccess' ); } public function onregistrationsuccess(getresponseuserevent $event) { $url = $this->router->generate('fos_user_registration_success'); $event->setresponse(new redirectresponse($url)); } }
and services.yml
services: usr_user.registration.success: class: usr\userbundle\eventlistener\registrationsuccesslistener arguments: ["@router"] tags: - { name: kernel.event_subscriber }
i getting error:
type error: argument 1 passed usr\userbundle\eventlistener\registrationsuccesslistener::onregistrationsuccess() must instance of fos\userbundle\event\getresponseuserevent, instance of fos\userbundle\event\formevent given
registrationcontroller.php
namespace usr\userbundle\controller; use fos\userbundle\fosuserevents; use fos\userbundle\event\formevent; use fos\userbundle\event\getresponseuserevent; use fos\userbundle\event\filteruserresponseevent; use symfony\bundle\frameworkbundle\controller\controller; use symfony\component\httpfoundation\request; use symfony\component\httpfoundation\redirectresponse; use symfony\component\httpkernel\exception\notfoundhttpexception; use symfony\component\security\core\exception\accessdeniedexception; use fos\userbundle\model\userinterface; use fos\userbundle\controller\registrationcontroller basecontroller; /** * controller managing registration */ class registrationcontroller extends basecontroller { public function registeraction(request $request) { /** @var $formfactory \fos\userbundle\form\factory\factoryinterface */ $formfactory = $this->get('fos_user.registration.form.factory'); /** @var $usermanager \fos\userbundle\model\usermanagerinterface */ $usermanager = $this->get('fos_user.user_manager'); /** @var $dispatcher \symfony\component\eventdispatcher\eventdispatcherinterface */ $dispatcher = $this->get('event_dispatcher'); $user = $usermanager->createuser(); $user->setenabled(true); $event = new getresponseuserevent($user, $request); $dispatcher->dispatch(fosuserevents::registration_initialize, $event); if (null !== $event->getresponse()) { return $event->getresponse(); } $form = $formfactory->createform(); $form->setdata($user); $form->handlerequest($request); if ($form->isvalid()) { $event = new formevent($form, $request); $dispatcher->dispatch(fosuserevents::registration_success, $event); $usermanager->updateuser($user); if (null === $response = $event->getresponse()) { $url = $this->generateurl('fos_user_registration_confirmed'); $response = new redirectresponse($url); } $dispatcher->dispatch(fosuserevents::registration_completed, new filteruserresponseevent($user, $request, $response)); return $response; } return $this->render('fosuserbundle:registration:register.html.twig', array( 'form' => $form->createview(), )); } /** * tell user check email provider */ public function checkemailaction() { $email = $this->get('session')->get('fos_user_send_confirmation_email/email'); $this->get('session')->remove('fos_user_send_confirmation_email/email'); $user = $this->get('fos_user.user_manager')->finduserbyemail($email); if (null === $user) { throw new notfoundhttpexception(sprintf('the user email "%s" not exist', $email)); } return $this->render('fosuserbundle:registration:checkemail.html.twig', array( 'user' => $user, )); } /** * receive confirmation token user email provider, login user */ public function confirmaction(request $request, $token) { /** @var $usermanager \fos\userbundle\model\usermanagerinterface */ $usermanager = $this->get('fos_user.user_manager'); $user = $usermanager->finduserbyconfirmationtoken($token); if (null === $user) { throw new notfoundhttpexception(sprintf('the user confirmation token "%s" not exist', $token)); } /** @var $dispatcher \symfony\component\eventdispatcher\eventdispatcherinterface */ $dispatcher = $this->get('event_dispatcher'); $user->setconfirmationtoken(null); $user->setenabled(true); $event = new getresponseuserevent($user, $request); $dispatcher->dispatch(fosuserevents::registration_confirm, $event); $usermanager->updateuser($user); if (null === $response = $event->getresponse()) { $url = $this->generateurl('fos_user_registration_confirmed'); $response = new redirectresponse($url); } $dispatcher->dispatch(fosuserevents::registration_confirmed, new filteruserresponseevent($user, $request, $response)); return $response; } /** * tell user account confirmed */ public function confirmedaction() { $user = $this->getuser(); if (!is_object($user) || !$user instanceof userinterface) { throw new accessdeniedexception('this user not have access section.'); } return $this->render('fosuserbundle:registration:confirmed.html.twig', array( 'user' => $user, 'targeturl' => $this->gettargeturlfromsession(), )); } private function gettargeturlfromsession() { // set securitycontext symfony <2.6 if (interface_exists('symfony\component\security\core\authentication\token\storage\tokenstorageinterface')) { $tokenstorage = $this->get('security.token_storage'); } else { $tokenstorage = $this->get('security.context'); } $key = sprintf('_security.%s.target_path', $tokenstorage->gettoken()->getproviderkey()); if ($this->get('session')->has($key)) { return $this->get('session')->get($key); } } public function registercuccessaction() { return $this->render('::base.html.twig', array()); } }
Comments
Post a Comment