Skip to content

Commit c6ee27d

Browse files
committed
minor #1624 simplifying controllers by using Symfony Controller shortcuts (gondo)
This PR was squashed before being merged into the 2.0.x-dev branch (closes #1624). Discussion ---------- simplifying controllers by using Symfony Controller shortcuts now, when all controllers extends Symfony `Controller` class, instead of ContainerAware as it was before, we can use advantage of `Controller`'s functions. also making sure that `$this->render(...)` syntax is consistent across all controllers Commits ------- 7dab23d simplifying controllers by using Symfony Controller shortcuts
2 parents 96e4542 + 7dab23d commit c6ee27d

6 files changed

+76
-70
lines changed

Controller/ChangePasswordController.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class ChangePasswordController extends Controller
3434
*/
3535
public function changePasswordAction(Request $request)
3636
{
37-
$user = $this->container->get('security.context')->getToken()->getUser();
37+
$user = $this->getUser();
3838
if (!is_object($user) || !$user instanceof UserInterface) {
3939
throw new AccessDeniedException('This user does not have access to this section.');
4040
}
4141

4242
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
43-
$dispatcher = $this->container->get('event_dispatcher');
43+
$dispatcher = $this->get('event_dispatcher');
4444

4545
$event = new GetResponseUserEvent($user, $request);
4646
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE, $event);
@@ -50,7 +50,7 @@ public function changePasswordAction(Request $request)
5050
}
5151

5252
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
53-
$formFactory = $this->container->get('fos_user.change_password.form.factory');
53+
$formFactory = $this->get('fos_user.change_password.form.factory');
5454

5555
$form = $formFactory->createForm();
5656
$form->setData($user);
@@ -59,15 +59,15 @@ public function changePasswordAction(Request $request)
5959

6060
if ($form->isValid()) {
6161
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
62-
$userManager = $this->container->get('fos_user.user_manager');
62+
$userManager = $this->get('fos_user.user_manager');
6363

6464
$event = new FormEvent($form, $request);
6565
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);
6666

6767
$userManager->updateUser($user);
6868

6969
if (null === $response = $event->getResponse()) {
70-
$url = $this->container->get('router')->generate('fos_user_profile_show');
70+
$url = $this->generateUrl('fos_user_profile_show');
7171
$response = new RedirectResponse($url);
7272
}
7373

@@ -76,9 +76,8 @@ public function changePasswordAction(Request $request)
7676
return $response;
7777
}
7878

79-
return $this->container->get('templating')->renderResponse(
80-
'FOSUserBundle:ChangePassword:changePassword.html.twig',
81-
array('form' => $form->createView())
82-
);
79+
return $this->render('FOSUserBundle:ChangePassword:changePassword.html.twig', array(
80+
'form' => $form->createView()
81+
));
8382
}
8483
}

Controller/GroupController.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ class GroupController extends Controller
3434
*/
3535
public function listAction()
3636
{
37-
$groups = $this->container->get('fos_user.group_manager')->findGroups();
37+
$groups = $this->get('fos_user.group_manager')->findGroups();
3838

39-
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:list.html.twig', array('groups' => $groups));
39+
return $this->render('FOSUserBundle:Group:list.html.twig', array(
40+
'groups' => $groups
41+
));
4042
}
4143

4244
/**
@@ -46,7 +48,9 @@ public function showAction($groupName)
4648
{
4749
$group = $this->findGroupBy('name', $groupName);
4850

49-
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:show.html.twig', array('group' => $group));
51+
return $this->render('FOSUserBundle:Group:show.html.twig', array(
52+
'group' => $group
53+
));
5054
}
5155

5256
/**
@@ -57,7 +61,7 @@ public function editAction(Request $request, $groupName)
5761
$group = $this->findGroupBy('name', $groupName);
5862

5963
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
60-
$dispatcher = $this->container->get('event_dispatcher');
64+
$dispatcher = $this->get('event_dispatcher');
6165

6266
$event = new GetResponseGroupEvent($group, $request);
6367
$dispatcher->dispatch(FOSUserEvents::GROUP_EDIT_INITIALIZE, $event);
@@ -67,7 +71,7 @@ public function editAction(Request $request, $groupName)
6771
}
6872

6973
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
70-
$formFactory = $this->container->get('fos_user.group.form.factory');
74+
$formFactory = $this->get('fos_user.group.form.factory');
7175

7276
$form = $formFactory->createForm();
7377
$form->setData($group);
@@ -76,15 +80,15 @@ public function editAction(Request $request, $groupName)
7680

7781
if ($form->isValid()) {
7882
/** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
79-
$groupManager = $this->container->get('fos_user.group_manager');
83+
$groupManager = $this->get('fos_user.group_manager');
8084

8185
$event = new FormEvent($form, $request);
8286
$dispatcher->dispatch(FOSUserEvents::GROUP_EDIT_SUCCESS, $event);
8387

8488
$groupManager->updateGroup($group);
8589

8690
if (null === $response = $event->getResponse()) {
87-
$url = $this->container->get('router')->generate('fos_user_group_show', array('groupName' => $group->getName()));
91+
$url = $this->generateUrl('fos_user_group_show', array('groupName' => $group->getName()));
8892
$response = new RedirectResponse($url);
8993
}
9094

@@ -93,7 +97,7 @@ public function editAction(Request $request, $groupName)
9397
return $response;
9498
}
9599

96-
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:edit.html.twig', array(
100+
return $this->render('FOSUserBundle:Group:edit.html.twig', array(
97101
'form' => $form->createview(),
98102
'group_name' => $group->getName(),
99103
));
@@ -105,11 +109,11 @@ public function editAction(Request $request, $groupName)
105109
public function newAction(Request $request)
106110
{
107111
/** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
108-
$groupManager = $this->container->get('fos_user.group_manager');
112+
$groupManager = $this->get('fos_user.group_manager');
109113
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
110-
$formFactory = $this->container->get('fos_user.group.form.factory');
114+
$formFactory = $this->get('fos_user.group.form.factory');
111115
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
112-
$dispatcher = $this->container->get('event_dispatcher');
116+
$dispatcher = $this->get('event_dispatcher');
113117

114118
$group = $groupManager->createGroup('');
115119

@@ -127,7 +131,7 @@ public function newAction(Request $request)
127131
$groupManager->updateGroup($group);
128132

129133
if (null === $response = $event->getResponse()) {
130-
$url = $this->container->get('router')->generate('fos_user_group_show', array('groupName' => $group->getName()));
134+
$url = $this->generateUrl('fos_user_group_show', array('groupName' => $group->getName()));
131135
$response = new RedirectResponse($url);
132136
}
133137

@@ -136,7 +140,7 @@ public function newAction(Request $request)
136140
return $response;
137141
}
138142

139-
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:new.html.twig', array(
143+
return $this->render('FOSUserBundle:Group:new.html.twig', array(
140144
'form' => $form->createview(),
141145
));
142146
}
@@ -147,12 +151,12 @@ public function newAction(Request $request)
147151
public function deleteAction(Request $request, $groupName)
148152
{
149153
$group = $this->findGroupBy('name', $groupName);
150-
$this->container->get('fos_user.group_manager')->deleteGroup($group);
154+
$this->get('fos_user.group_manager')->deleteGroup($group);
151155

152-
$response = new RedirectResponse($this->container->get('router')->generate('fos_user_group_list'));
156+
$response = new RedirectResponse($this->generateUrl('fos_user_group_list'));
153157

154158
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
155-
$dispatcher = $this->container->get('event_dispatcher');
159+
$dispatcher = $this->get('event_dispatcher');
156160
$dispatcher->dispatch(FOSUserEvents::GROUP_DELETE_COMPLETED, new FilterGroupResponseEvent($group, $request, $response));
157161

158162
return $response;
@@ -170,7 +174,7 @@ public function deleteAction(Request $request, $groupName)
170174
protected function findGroupBy($key, $value)
171175
{
172176
if (!empty($value)) {
173-
$group = $this->container->get('fos_user.group_manager')->{'findGroupBy'.ucfirst($key)}($value);
177+
$group = $this->get('fos_user.group_manager')->{'findGroupBy'.ucfirst($key)}($value);
174178
}
175179

176180
if (empty($group)) {

Controller/ProfileController.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,28 @@ class ProfileController extends Controller
3333
*/
3434
public function showAction()
3535
{
36-
$user = $this->container->get('security.context')->getToken()->getUser();
36+
$user = $this->getUser();
3737
if (!is_object($user) || !$user instanceof UserInterface) {
3838
throw new AccessDeniedException('This user does not have access to this section.');
3939
}
4040

41-
return $this->container->get('templating')->renderResponse('FOSUserBundle:Profile:show.html.twig', array('user' => $user));
41+
return $this->render('FOSUserBundle:Profile:show.html.twig', array(
42+
'user' => $user
43+
));
4244
}
4345

4446
/**
4547
* Edit the user
4648
*/
4749
public function editAction(Request $request)
4850
{
49-
$user = $this->container->get('security.context')->getToken()->getUser();
51+
$user = $this->getUser();
5052
if (!is_object($user) || !$user instanceof UserInterface) {
5153
throw new AccessDeniedException('This user does not have access to this section.');
5254
}
5355

5456
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
55-
$dispatcher = $this->container->get('event_dispatcher');
57+
$dispatcher = $this->get('event_dispatcher');
5658

5759
$event = new GetResponseUserEvent($user, $request);
5860
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
@@ -62,7 +64,7 @@ public function editAction(Request $request)
6264
}
6365

6466
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
65-
$formFactory = $this->container->get('fos_user.profile.form.factory');
67+
$formFactory = $this->get('fos_user.profile.form.factory');
6668

6769
$form = $formFactory->createForm();
6870
$form->setData($user);
@@ -71,15 +73,15 @@ public function editAction(Request $request)
7173

7274
if ($form->isValid()) {
7375
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
74-
$userManager = $this->container->get('fos_user.user_manager');
76+
$userManager = $this->get('fos_user.user_manager');
7577

7678
$event = new FormEvent($form, $request);
7779
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
7880

7981
$userManager->updateUser($user);
8082

8183
if (null === $response = $event->getResponse()) {
82-
$url = $this->container->get('router')->generate('fos_user_profile_show');
84+
$url = $this->generateUrl('fos_user_profile_show');
8385
$response = new RedirectResponse($url);
8486
}
8587

@@ -88,9 +90,8 @@ public function editAction(Request $request)
8890
return $response;
8991
}
9092

91-
return $this->container->get('templating')->renderResponse(
92-
'FOSUserBundle:Profile:edit.html.twig',
93-
array('form' => $form->createView())
94-
);
93+
return $this->render('FOSUserBundle:Profile:edit.html.twig', array(
94+
'form' => $form->createView()
95+
));
9596
}
9697
}

Controller/RegistrationController.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class RegistrationController extends Controller
3333
public function registerAction(Request $request)
3434
{
3535
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
36-
$formFactory = $this->container->get('fos_user.registration.form.factory');
36+
$formFactory = $this->get('fos_user.registration.form.factory');
3737
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
38-
$userManager = $this->container->get('fos_user.user_manager');
38+
$userManager = $this->get('fos_user.user_manager');
3939
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
40-
$dispatcher = $this->container->get('event_dispatcher');
40+
$dispatcher = $this->get('event_dispatcher');
4141

4242
$user = $userManager->createUser();
4343
$user->setEnabled(true);
@@ -61,7 +61,7 @@ public function registerAction(Request $request)
6161
$userManager->updateUser($user);
6262

6363
if (null === $response = $event->getResponse()) {
64-
$url = $this->container->get('router')->generate('fos_user_registration_confirmed');
64+
$url = $this->generateUrl('fos_user_registration_confirmed');
6565
$response = new RedirectResponse($url);
6666
}
6767

@@ -70,7 +70,7 @@ public function registerAction(Request $request)
7070
return $response;
7171
}
7272

73-
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.twig', array(
73+
return $this->render('FOSUserBundle:Registration:register.html.twig', array(
7474
'form' => $form->createView(),
7575
));
7676
}
@@ -80,15 +80,15 @@ public function registerAction(Request $request)
8080
*/
8181
public function checkEmailAction()
8282
{
83-
$email = $this->container->get('session')->get('fos_user_send_confirmation_email/email');
84-
$this->container->get('session')->remove('fos_user_send_confirmation_email/email');
85-
$user = $this->container->get('fos_user.user_manager')->findUserByEmail($email);
83+
$email = $this->get('session')->get('fos_user_send_confirmation_email/email');
84+
$this->get('session')->remove('fos_user_send_confirmation_email/email');
85+
$user = $this->get('fos_user.user_manager')->findUserByEmail($email);
8686

8787
if (null === $user) {
8888
throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
8989
}
9090

91-
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:checkEmail.html.twig', array(
91+
return $this->render('FOSUserBundle:Registration:checkEmail.html.twig', array(
9292
'user' => $user,
9393
));
9494
}
@@ -99,7 +99,7 @@ public function checkEmailAction()
9999
public function confirmAction(Request $request, $token)
100100
{
101101
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
102-
$userManager = $this->container->get('fos_user.user_manager');
102+
$userManager = $this->get('fos_user.user_manager');
103103

104104
$user = $userManager->findUserByConfirmationToken($token);
105105

@@ -108,7 +108,7 @@ public function confirmAction(Request $request, $token)
108108
}
109109

110110
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
111-
$dispatcher = $this->container->get('event_dispatcher');
111+
$dispatcher = $this->get('event_dispatcher');
112112

113113
$user->setConfirmationToken(null);
114114
$user->setEnabled(true);
@@ -119,7 +119,7 @@ public function confirmAction(Request $request, $token)
119119
$userManager->updateUser($user);
120120

121121
if (null === $response = $event->getResponse()) {
122-
$url = $this->container->get('router')->generate('fos_user_registration_confirmed');
122+
$url = $this->generateUrl('fos_user_registration_confirmed');
123123
$response = new RedirectResponse($url);
124124
}
125125

@@ -133,12 +133,12 @@ public function confirmAction(Request $request, $token)
133133
*/
134134
public function confirmedAction()
135135
{
136-
$user = $this->container->get('security.context')->getToken()->getUser();
136+
$user = $this->getUser();
137137
if (!is_object($user) || !$user instanceof UserInterface) {
138138
throw new AccessDeniedException('This user does not have access to this section.');
139139
}
140140

141-
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:confirmed.html.twig', array(
141+
return $this->render('FOSUserBundle:Registration:confirmed.html.twig', array(
142142
'user' => $user,
143143
));
144144
}

0 commit comments

Comments
 (0)