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