overriding_controllers.rst 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. Overriding Default FOSUserBundle Controllers
  2. ============================================
  3. The default controllers packaged with the FOSUserBundle provide a lot of
  4. functionality that is sufficient for general use cases. But, you might find
  5. that you need to extend that functionality and add some logic that suits the
  6. specific needs of your application.
  7. The first step to overriding a controller in the bundle is to create a child
  8. bundle whose parent is FOSUserBundle. The following code snippet creates a new
  9. bundle named ``AcmeUserBundle`` that declares itself a child of FOSUserBundle.
  10. .. code-block:: php
  11. <?php
  12. // src/Acme/UserBundle/AcmeUserBundle.php
  13. namespace Acme\UserBundle;
  14. use Symfony\Component\HttpKernel\Bundle\Bundle;
  15. class AcmeUserBundle extends Bundle
  16. {
  17. public function getParent()
  18. {
  19. return 'FOSUserBundle';
  20. }
  21. }
  22. .. note::
  23. The Symfony Framework only allows a bundle to have one child. You cannot
  24. create another bundle that is also a child of FOSUserBundle.
  25. Now that you have created the new child bundle you can simply create a controller class
  26. with the same name and in the same location as the one you want to override. This
  27. example overrides the ``RegistrationController`` by extending the FOSUserBundle
  28. ``RegistrationController`` class and simply overriding the method that needs the extra
  29. functionality.
  30. The example below overrides the ``registerAction`` method. It uses the code from
  31. the base controller and adds logging a new user registration to it.
  32. .. code-block:: php
  33. <?php
  34. // src/Acme/UserBundle/Controller/RegistrationController.php
  35. namespace Acme\UserBundle\Controller;
  36. use Symfony\Component\HttpFoundation\RedirectResponse;
  37. use FOS\UserBundle\Controller\RegistrationController as BaseController;
  38. class RegistrationController extends BaseController
  39. {
  40. public function registerAction()
  41. {
  42. $form = $this->container->get('fos_user.registration.form');
  43. $formHandler = $this->container->get('fos_user.registration.form.handler');
  44. $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
  45. $process = $formHandler->process($confirmationEnabled);
  46. if ($process) {
  47. $user = $form->getData();
  48. /*****************************************************
  49. * Add new functionality (e.g. log the registration) *
  50. *****************************************************/
  51. $this->container->get('logger')->info(
  52. sprintf('New user registration: %s', $user)
  53. );
  54. if ($confirmationEnabled) {
  55. $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
  56. $route = 'fos_user_registration_check_email';
  57. } else {
  58. $this->authenticateUser($user);
  59. $route = 'fos_user_registration_confirmed';
  60. }
  61. $this->setFlash('fos_user_success', 'registration.flash.user_created');
  62. $url = $this->container->get('router')->generate($route);
  63. return new RedirectResponse($url);
  64. }
  65. return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.twig', array(
  66. 'form' => $form->createView(),
  67. ));
  68. }
  69. }
  70. .. note::
  71. If you do not extend the FOSUserBundle controller class that you want
  72. to override and instead extend ContainerAware or the Controller class
  73. provided by the FrameworkBundle then you must implement all of the methods
  74. of the FOSUserBundle controller that you are overriding.