php - Overriding FOSUserBUndle Controllers Symfony 2 -
i want override fos\userbundle\controller\registrationcontroller add functionalities (to manage fields add in registration form etc).
i not know why, after overriding it, symphony ignores controller. not first time, tried override others ... never found solutions ...
<?php namespace fp\userbundle\controller; use symfony\component\httpfoundation\redirectresponse; use fos\userbundle\controller\registrationcontroller basecontroller; class registrationcontroller extends basecontroller { public function registeraction() { $response = parent::registeraction(); echo "fpuserbundle"; // custom stuff return $response; } }
.
<?php namespace fp\userbundle; use symfony\component\httpkernel\bundle\bundle; class fpuserbundle extends bundle { public function getparent() { return 'fosuserbundle'; } }
.
<?php /* * file part of fosuserbundle package. * * (c) friendsofsymfony <http://friendsofsymfony.github.com/> * * full copyright , license information, please view license * file distributed source code. */ namespace fos\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; /** * controller managing registration * * @author thibault duplessis <thibault.duplessis@gmail.com> * @author christophe coevoet <stof@notk.org> */ class registrationcontroller extends controller { public function registeraction(request $request) { echo "fosuserbundle"; /** @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);//active l'user $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); //--- ajout des données pour les champs ajoutés --- $user->setdateinscrip(new \datetime()); $user->setroles(array('role_user')); //--------- fin de l'ajout --------- $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->redirect($this->generateurl('fp_user_inscrip', array('user' => $user))); /* return $this->render('fpplatformbundle:index:index.html.twig', array( 'user' => $user, ));*/ } }
despite these codes, when run check, "fpuserbundle" not displayed, while "fosuserbundle" appears ...
overriding classes (controllers classes too!) give new class futures base-class has (except direct access private methods or properties). not forget base-class still can used beside new class. dependence's class instantiate.
$a = new baseclass();
vs
$a = new newclass();
so question 1 symfony use? , can manage routing. long in app/config/routing.yml:
fos_user: resource: "@fosuserbundle/resources/config/routing/all.xml"
symfony use original fosuserbundle controllers. find xml files in vendor/fos.. directory , copy them own project. change above showed rule own bundle , change controllernames in xml files. of course write own .yml files too.
Comments
Post a Comment