我想重写FOSUserBundleControllerRegistrationController以添加一些功能(管理我在注册表单中添加的字段等)。
我不知道为什么,在重写它之后,symphony忽略了我的控制器。这不是第一次了,我也试图凌驾于别人之上…从未找到解决方案…
<?php
namespace FPUserBundleController;
use SymfonyComponentHttpFoundationRedirectResponse;
use FOSUserBundleControllerRegistrationController as BaseController;
class RegistrationController extends BaseController
{
public function registerAction()
{
$response = parent::registerAction();
echo "FPUserBundle";
// do custom stuff
return $response;
}
}
.
<?php
namespace FPUserBundle;
use SymfonyComponentHttpKernelBundleBundle;
class FPUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
.
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOSUserBundleController;
use FOSUserBundleFOSUserEvents;
use FOSUserBundleEventFormEvent;
use FOSUserBundleEventGetResponseUserEvent;
use FOSUserBundleEventFilterUserResponseEvent;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
use SymfonyComponentSecurityCoreExceptionAccessDeniedException;
use FOSUserBundleModelUserInterface;
/**
* Controller managing the 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 FOSUserBundleFormFactoryFactoryInterface */
$formFactory = $this->get('fos_user.registration.form.factory');
/** @var $userManager FOSUserBundleModelUserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
/** @var $dispatcher SymfonyComponentEventDispatcherEventDispatcherInterface */
$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 the user to check his 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 with email "%s" does not exist', $email));
}
return $this->render('FOSUserBundle:Registration:checkEmail.html.twig', array(
'user' => $user,
));
}
/**
* Receive the confirmation token from user email provider, login the user
*/
public function confirmAction(Request $request, $token)
{
/** @var $userManager FOSUserBundleModelUserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserByConfirmationToken($token);
if (null === $user) {
throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
}
/** @var $dispatcher SymfonyComponentEventDispatcherEventDispatcherInterface */
$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 the user his account is now confirmed
*/
public function confirmedAction()
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->redirect($this->generateUrl('fp_user_inscrip', array('user' => $user)));
/*
return $this->render('FPPlatformBundle:Index:index.html.twig', array(
'user' => $user,
));*/
}
}
尽管有这些代码,当我运行检查时,"FPUserBundle"没有显示,而"FOSUserBundle"看起来很好…
检查是否已将新的Bundle添加到app/AppKernel.php::registerBundles函数中
重写类(控制器也是类!)将为新类提供基类拥有的所有未来(除了直接访问私有方法或属性)。但是不要忘记,基类仍然可以在新类旁边使用。它依赖于你实例化的类。
$a = new BaseClass();
和
$a = new NewClass();
那么问题是Symfony将使用哪一个?这就是你可以通过路由来管理的。只要在app/config/routing.yml:
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
Symfony将使用原来的FOSUserBundle控制器。只要在vendor/FOS中找到那些xml文件就行了。目录并将它们复制到您自己的项目中。将上面显示的规则更改为您自己的bundle,并更改xml文件中的控制器名。当然你也可以写自己的。yml文件。
阅读更多