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

14 files changed

+75
-71
lines changed

book/controller.rst

+30-16
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

+1-1
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

+3-3
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

+4
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

+13-11
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

+1-1
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

+1-1
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

+3-3
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

+1-1
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

+2-4
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
}

cookbook/form/direct_submit.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ submissions::
2525
if ($form->isValid()) {
2626
// perform some action...
2727

28-
return $this->redirect($this->generateUrl('task_success'));
28+
return $this->redirectToRoute('task_success');
2929
}
3030

3131
return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
@@ -66,7 +66,7 @@ method, pass the submitted data directly to
6666
if ($form->isValid()) {
6767
// perform some action...
6868

69-
return $this->redirect($this->generateUrl('task_success'));
69+
return $this->redirectToRoute('task_success');
7070
}
7171
}
7272

@@ -111,7 +111,7 @@ a convenient shortcut to the previous example::
111111
if ($form->isValid()) {
112112
// perform some action...
113113

114-
return $this->redirect($this->generateUrl('task_success'));
114+
return $this->redirectToRoute('task_success');
115115
}
116116
}
117117

cookbook/form/form_collections.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ the relationship between the removed ``Tag`` and ``Task`` object.
717717
$em->flush();
718718

719719
// redirect back to some edit page
720-
return $this->redirect($this->generateUrl('task_edit', array('id' => $id)));
720+
return $this->redirectToRoute('task_edit', array('id' => $id));
721721
}
722722

723723
// render some form template

cookbook/security/remember_me.rst

+1-9
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,11 @@ In the following example, the action is only allowed if the user has the
162162
163163
public function editAction()
164164
{
165-
if (false === $this->get('security.authorization_checker')->isGranted(
166-
'IS_AUTHENTICATED_FULLY'
167-
)) {
168-
throw new AccessDeniedException();
169-
}
165+
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
170166
171167
// ...
172168
}
173169
174-
.. versionadded:: 2.6
175-
The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior
176-
to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service.
177-
178170
If your application is based on the Symfony Standard Edition, you can also secure
179171
your controller using annotations:
180172

cookbook/security/securing_services.rst

+11-17
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,34 @@
55
How to Secure any Service or Method in your Application
66
=======================================================
77

8-
In the security chapter, you can see how to :ref:`secure a controller <book-security-securing-controller>`
9-
by requesting the ``security.authorization_checker`` service from the Service Container
10-
and checking the current user's role::
8+
In the security chapter, you can see how to
9+
:ref:`secure a controller <book-security-securing-controller>` by requesting
10+
the ``security.authorization_checker`` service from the Service Container and
11+
checking the current user's role::
1112

1213
// ...
1314
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1415

1516
public function helloAction($name)
1617
{
17-
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
18-
throw new AccessDeniedException();
19-
}
18+
$this->denyAccessUnlessGranted('ROLE_ADMIN');
2019

2120
// ...
2221
}
2322

24-
.. versionadded:: 2.6
25-
The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior
26-
to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service.
27-
28-
You can also secure *any* service in a similar way by injecting the ``security.authorization_checker``
23+
You can also secure *any* service by injecting the ``security.authorization_checker``
2924
service into it. For a general introduction to injecting dependencies into
3025
services see the :doc:`/book/service_container` chapter of the book. For
3126
example, suppose you have a ``NewsletterManager`` class that sends out emails
32-
and you want to restrict its use to only users who have some ``ROLE_NEWSLETTER_ADMIN``
33-
role. Before you add security, the class looks something like this:
34-
35-
.. code-block:: php
27+
and you want to restrict its use to only users who have some
28+
``ROLE_NEWSLETTER_ADMIN`` role. Before you add security, the class looks
29+
something like this::
3630

3731
// src/AppBundle/Newsletter/NewsletterManager.php
3832
namespace AppBundle\Newsletter;
3933

4034
class NewsletterManager
4135
{
42-
4336
public function sendNewsletter()
4437
{
4538
// ... where you actually do the work
@@ -55,8 +48,9 @@ check, this is an ideal candidate for constructor injection, which guarantees
5548
that the authorization checker object will be available inside the ``NewsletterManager``
5649
class::
5750

58-
namespace AppBundle\Newsletter;
51+
// src/AppBundle/Newsletter/NewsletterManager.php
5952

53+
// ...
6054
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
6155

6256
class NewsletterManager

0 commit comments

Comments
 (0)