Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 643c458

Browse filesBrowse files
Cydonia7wouterj
authored andcommitted
redirect changed to redirectToRoute
1 parent 7ae62e8 commit 643c458
Copy full SHA for 643c458

File tree

Expand file treeCollapse file tree

10 files changed

+21
-34
lines changed
Filter options
Expand file treeCollapse file tree

10 files changed

+21
-34
lines changed

‎book/controller.rst

Copy file name to clipboardExpand all lines: book/controller.rst
+6-19Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -429,43 +429,30 @@ 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');
439437
}
440438

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.
444-
445-
By default, the ``redirect()`` method performs a 302 (temporary) redirect. To
439+
By default, the ``redirectToRoute()`` method performs a 302 (temporary) redirect. To
446440
perform a 301 (permanent) redirect, modify the second argument::
447441

448442
public function indexAction()
449443
{
450-
return $this->redirect($this->generateUrl('homepage'), 301);
444+
return $this->redirectToRoute('homepage', 301);
451445
}
452446

453447
.. tip::
454448

455-
The ``redirect()`` method is simply a shortcut that creates a ``Response``
449+
The ``redirectToRoute()`` method is simply a shortcut that creates a ``Response``
456450
object that specializes in redirecting the user. It's equivalent to::
457451

458452
use Symfony\Component\HttpFoundation\RedirectResponse;
459453

460454
return new RedirectResponse($this->generateUrl('homepage'));
461455

462-
.. versionadded:: 2.6
463-
You can also directly use
464-
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller::redirectToRoute`
465-
and give it directly the route name like :
466-
467-
return $this->redirectToRoute('homepage');
468-
469456
.. index::
470457
single: Controller; Rendering templates
471458

@@ -635,7 +622,7 @@ For example, imagine you're processing a form submit::
635622
'Your changes were saved!'
636623
);
637624

638-
return $this->redirect($this->generateUrl(...));
625+
return $this->redirectToRoute(...);
639626
}
640627

641628
return $this->render(...);

‎book/doctrine.rst

Copy file name to clipboardExpand all lines: book/doctrine.rst
+1-1Lines 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

Copy file name to clipboardExpand all lines: book/forms.rst
+3-3Lines 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

Copy file name to clipboardExpand all lines: book/propel.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ have a route that maps a product id to an update action in a controller::
234234
$product->setName('New product name!');
235235
$product->save();
236236

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

240240
Updating an object involves just three steps:

‎book/validation.rst

Copy file name to clipboardExpand all lines: book/validation.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ workflow looks like the following from inside a controller::
231231
if ($form->isValid()) {
232232
// the validation passed, do something with the $author object
233233

234-
return $this->redirect($this->generateUrl(...));
234+
return $this->redirectToRoute(...);
235235
}
236236

237237
return $this->render('BlogBundle:Author:form.html.twig', array(

‎components/form/introduction.rst

Copy file name to clipboardExpand all lines: components/form/introduction.rst
+1-1Lines 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

Copy file name to clipboardExpand all lines: cookbook/doctrine/file_uploads.rst
+3-3Lines 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

Copy file name to clipboardExpand all lines: cookbook/doctrine/registration_form.rst
+1-1Lines 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/form/direct_submit.rst

Copy file name to clipboardExpand all lines: cookbook/form/direct_submit.rst
+3-3Lines changed: 3 additions & 3 deletions
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

Copy file name to clipboardExpand all lines: cookbook/form/form_collections.rst
+1-1Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.