Skip to content

Commit 3405c42

Browse files
committed
feature #5046 Rebased "add shortcut methods" (Cydonia7, WouterJ)
This PR was merged into the 2.6 branch. Discussion ---------- Rebased "add shortcut methods" Replaces #4109 Original Pr description: > | Q | A > | ------------- | --- > | Doc fix? | no > | New docs? | yes (symfony/symfony#11593) > | Applies to | 2.6+ > | Fixed tickets | #4666 > > This commit is associated to symfony/symfony#11593 that adds new shortcut methods to controllers. > > If anything is wrong with my commit or this description, please tell me and I will fix it as soon as possible. I'm glad I finally try to help this awesome project. Commits ------- 994ed3a Little fixes f807d14 Fixes 5b015f2 Modifications according to comments 7f9bc8c And now the same for isGranted where it is possible 46e2505 Changed to addFlash where it is possible (ie in controllers) 643c458 redirect changed to redirectToRoute 7ae62e8 Minor improvements 4611ce9 Minor format improvements 3bcb186 Added shortcut methods for controllers
2 parents 2035d62 + 994ed3a commit 3405c42

File tree

14 files changed

+75
-71
lines changed

14 files changed

+75
-71
lines changed

book/controller.rst

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -429,35 +429,47 @@ A great way to see the core functionality in action is to look in the
429429
Redirecting
430430
~~~~~~~~~~~
431431

432-
If you want to redirect the user to another page, use the
433-
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::redirect`
434-
method::
432+
If you want to redirect the user to another page, use the ``redirectToRoute()`` method::
435433

436434
public function indexAction()
437435
{
438-
return $this->redirect($this->generateUrl('homepage'));
436+
return $this->redirectToRoute('homepage');
437+
438+
// redirectToRoute is equivalent to using redirect() and generateUrl() together:
439+
// return $this->redirect($this->generateUrl('homepage'), 301);
439440
}
440441

441-
The ``generateUrl()`` method is just a helper function that generates the URL
442-
for a given route. For more information, see the :doc:`Routing </book/routing>`
443-
chapter.
442+
.. versionadded:: 2.6
443+
The ``redirectToRoute()`` method was added in Symfony 2.6. Previously (and still now), you
444+
could use ``redirect()`` and ``generateUrl()`` together for this (see the example above).
445+
446+
Or, if you want to redirect externally, just use ``redirect()`` and pass it the URL::
447+
448+
public function indexAction()
449+
{
450+
return $this->redirect('http://symfony.com/doc');
451+
}
444452

445-
By default, the ``redirect()`` method performs a 302 (temporary) redirect. To
453+
By default, the ``redirectToRoute()`` method performs a 302 (temporary) redirect. To
446454
perform a 301 (permanent) redirect, modify the second argument::
447455

448456
public function indexAction()
449457
{
450-
return $this->redirect($this->generateUrl('homepage'), 301);
458+
return $this->redirectToRoute('homepage', array(), 301);
451459
}
452460

453461
.. tip::
454462

455-
The ``redirect()`` method is simply a shortcut that creates a ``Response``
456-
object that specializes in redirecting the user. It's equivalent to::
463+
The ``redirectToRoute()`` method is simply a shortcut that creates a
464+
``Response`` object that specializes in redirecting the user. It's
465+
equivalent to::
457466

458467
use Symfony\Component\HttpFoundation\RedirectResponse;
459468

460-
return new RedirectResponse($this->generateUrl('homepage'));
469+
public function indexAction()
470+
{
471+
return new RedirectResponse($this->generateUrl('homepage'));
472+
}
461473

462474
.. index::
463475
single: Controller; Rendering templates
@@ -623,12 +635,14 @@ For example, imagine you're processing a form submit::
623635
if ($form->isValid()) {
624636
// do some sort of processing
625637

626-
$request->getSession()->getFlashBag()->add(
638+
$this->addFlash(
627639
'notice',
628640
'Your changes were saved!'
629641
);
630642

631-
return $this->redirect($this->generateUrl(...));
643+
// $this->addFlash is equivalent to $this->get('session')->getFlashBag()->add
644+
645+
return $this->redirectToRoute(...);
632646
}
633647

634648
return $this->render(...);
@@ -638,8 +652,8 @@ After processing the request, the controller sets a ``notice`` flash message
638652
in the session and then redirects. The name (``notice``) isn't significant -
639653
it's just something you invent and reference next.
640654

641-
In the template of the next page (or even better, in your base layout template),
642-
the following code will render the ``notice`` message:
655+
In the template of the next action, the following code could be used to render
656+
the ``notice`` message:
643657

644658
.. configuration-block::
645659

book/doctrine.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ you have a route that maps a product id to an update action in a controller::
667667
$product->setName('New product name!');
668668
$em->flush();
669669

670-
return $this->redirect($this->generateUrl('homepage'));
670+
return $this->redirectToRoute('homepage');
671671
}
672672

673673
Updating an object involves just three steps:

book/forms.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ controller::
234234
if ($form->isValid()) {
235235
// perform some action, such as saving the task to the database
236236

237-
return $this->redirect($this->generateUrl('task_success'));
237+
return $this->redirectToRoute('task_success');
238238
}
239239

240240
// ...
@@ -319,7 +319,7 @@ querying if the "Save and add" button was clicked::
319319
? 'task_new'
320320
: 'task_success';
321321

322-
return $this->redirect($this->generateUrl($nextAction));
322+
return $this->redirectToRoute($nextAction);
323323
}
324324

325325
.. index::
@@ -1233,7 +1233,7 @@ it after a form submission can be done when the form is valid::
12331233
$em->persist($task);
12341234
$em->flush();
12351235

1236-
return $this->redirect($this->generateUrl('task_success'));
1236+
return $this->redirectToRoute('task_success');
12371237
}
12381238

12391239
If, for some reason, you don't have access to your original ``$task`` object,

book/propel.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,15 @@ have a route that maps a product id to an update action in a controller::
241241
);
242242
}
243243

244+
<<<<<<< HEAD
244245
$product->setName('New product name!');
245246
$product->save();
246247

247248
return $this->redirect($this->generateUrl('homepage'));
248249
}
250+
=======
251+
return $this->redirectToRoute('homepage');
252+
>>>>>>> pull/5046
249253
}
250254

251255
Updating an object involves just three steps:

book/security.rst

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -813,23 +813,25 @@ You can easily deny access from inside a controller::
813813

814814
public function helloAction($name)
815815
{
816-
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
817-
throw $this->createAccessDeniedException();
818-
}
816+
// The second parameter is used to specify on what object the role is tested.
817+
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
818+
819+
// Old way :
820+
// if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
821+
// throw $this->createAccessDeniedException('Unable to access this page!');
822+
// }
819823

820824
// ...
821825
}
822826

823827
.. versionadded:: 2.6
824-
The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior
825-
to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service.
826-
827-
.. versionadded:: 2.5
828-
The ``createAccessDeniedException`` method was introduced in Symfony 2.5.
828+
The ``denyAccessUnlessGranted()`` method was introduced in Symfony 2.6. Previously (and
829+
still now), you could check access directly and throw the ``AccessDeniedException`` as shown
830+
in the example above).
829831

830-
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createAccessDeniedException`
831-
method creates a special :class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException`
832-
object, which ultimately triggers a 403 HTTP response inside Symfony.
832+
In both cases, a special
833+
:class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException`
834+
is thrown, which ultimately triggers a 403 HTTP response inside Symfony.
833835

834836
That's it! If the user isn't logged in yet, they will be asked to login (e.g.
835837
redirected to the login page). If they *are* logged in, they'll be shown

book/validation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ workflow looks like the following from inside a controller::
232232
if ($form->isValid()) {
233233
// the validation passed, do something with the $author object
234234

235-
return $this->redirect($this->generateUrl(...));
235+
return $this->redirectToRoute(...);
236236
}
237237

238238
return $this->render('author/form.html.twig', array(

components/form/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ method:
587587
588588
// ... perform some action, such as saving the data to the database
589589
590-
return $this->redirect($this->generateUrl('task_success'));
590+
return $this->redirectToRoute('task_success');
591591
}
592592
593593
// ...

cookbook/doctrine/file_uploads.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ The following controller shows you how to handle the entire process::
244244
$em->persist($document);
245245
$em->flush();
246246

247-
return $this->redirect($this->generateUrl(...));
247+
return $this->redirectToRoute(...);
248248
}
249249

250250
return array('form' => $form->createView());
@@ -267,7 +267,7 @@ in a moment to handle the file upload::
267267
$em->persist($document);
268268
$em->flush();
269269

270-
return $this->redirect(...);
270+
return $this->redirectToRoute(...);
271271
}
272272

273273
The ``upload()`` method will take advantage of the :class:`Symfony\\Component\\HttpFoundation\\File\\UploadedFile`
@@ -432,7 +432,7 @@ call to ``$document->upload()`` should be removed from the controller::
432432
$em->persist($document);
433433
$em->flush();
434434

435-
return $this->redirect(...);
435+
return $this->redirectToRoute(...);
436436
}
437437

438438
.. note::

cookbook/doctrine/registration_form.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ the validation and saves the data into the database::
287287
$em->persist($registration->getUser());
288288
$em->flush();
289289

290-
return $this->redirect(...);
290+
return $this->redirectToRoute(...);
291291
}
292292

293293
return $this->render(

cookbook/expression/expressions.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ accepts an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` object::
3333

3434
public function indexAction()
3535
{
36-
if (!$this->get('security.authorization_checker')->isGranted(new Expression(
36+
$this->denyAccessUnlessGranted(new Expression(
3737
'"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())'
38-
))) {
39-
throw $this->createAccessDeniedException();
40-
}
38+
));
4139

4240
// ...
4341
}

0 commit comments

Comments
 (0)