Skip to content

Commit a2efe87

Browse files
authored
Merge pull request #7201 from kenjis/docs-improve-redirect
docs: improve `redirect()` explanation
2 parents ea1b628 + ff43b9f commit a2efe87

9 files changed

Lines changed: 58 additions & 32 deletions

File tree

system/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ function old(string $key, $default = null, $escape = 'html')
863863
*
864864
* If more control is needed, you must use $response->redirect explicitly.
865865
*
866-
* @param string $route
866+
* @param string|null $route Route name or Controller::method
867867
*/
868868
function redirect(?string $route = null): RedirectResponse
869869
{

system/HTTP/RedirectResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RedirectResponse extends Response
2424
* Sets the URI to redirect to and, optionally, the HTTP status code to use.
2525
* If no code is provided it will be automatically determined.
2626
*
27-
* @param string $uri The URI to redirect to
27+
* @param string $uri The URI path (relative to baseURL) to redirect to
2828
* @param int|null $code HTTP status code
2929
*
3030
* @return $this
@@ -44,7 +44,7 @@ public function to(string $uri, ?int $code = null, string $method = 'auto')
4444
* Sets the URI to redirect to but as a reverse-routed or named route
4545
* instead of a raw URI.
4646
*
47-
* @param string $route Named route or Controller::method
47+
* @param string $route Route name or Controller::method
4848
*
4949
* @return $this
5050
*

system/Router/RouteCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
10431043
* // Equals 'path/$param1/$param2'
10441044
* reverseRoute('Controller::method', $param1, $param2);
10451045
*
1046-
* @param string $search Named route or Controller::method
1046+
* @param string $search Route name or Controller::method
10471047
* @param int|string ...$params One or more parameters to be passed to the route.
10481048
* The last parameter allows you to set the locale.
10491049
*

user_guide_src/source/general/common_functions.rst

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,26 +317,48 @@ Miscellaneous Functions
317317

318318
.. php:function:: redirect(string $route)
319319
320-
:param string $route: The reverse-routed or named route to redirect the user to.
320+
:param string $route: The route name or Controller::method to redirect the user to.
321321
:rtype: RedirectResponse
322322

323323
.. important:: When you use this function, an instance of ``RedirectResponse`` must be returned
324324
in the method of the :doc:`Controller <../incoming/controllers>` or
325325
the :doc:`Controller Filter <../incoming/filters>`. If you forget to return it,
326326
no redirection will occur.
327327

328-
Returns a RedirectResponse instance allowing you to easily create redirects:
328+
Returns a RedirectResponse instance allowing you to easily create redirects.
329+
330+
**Redirect to a URI path**
331+
332+
When you want to pass a URI path (relative to baseURL), use ``redirect()->to()``:
329333

330334
.. literalinclude:: common_functions/005.php
335+
:lines: 2-
331336

332-
.. note:: ``redirect()->back()`` is not the same as browser "back" button.
333-
It takes a visitor to "the last page viewed during the Session" when the Session is available.
334-
If the Session hasn’t been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.
337+
**Redirect to a Defined Route**
338+
339+
When you want to pass a :ref:`route name <using-named-routes>` or Controller::method
340+
for :ref:`reverse routing <reverse-routing>`, use ``redirect()->route()``:
341+
342+
.. literalinclude:: common_functions/013.php
343+
:lines: 2-
335344

336-
When passing an argument into the function, it is treated as a named/reverse-routed route, not a relative/full URI,
345+
When passing an argument into the function, it is treated as a route name or
346+
Controller::method for reverse routing, not a relative/full URI,
337347
treating it the same as using ``redirect()->route()``:
338348

339349
.. literalinclude:: common_functions/006.php
350+
:lines: 2-
351+
352+
**Redirect Back**
353+
354+
When you want to redirect back, use ``redirect()->back()``:
355+
356+
.. literalinclude:: common_functions/014.php
357+
:lines: 2-
358+
359+
.. note:: ``redirect()->back()`` is not the same as browser "back" button.
360+
It takes a visitor to "the last page viewed during the Session" when the Session is available.
361+
If the Session hasn’t been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.
340362

341363
.. php:function:: remove_invisible_characters($str[, $urlEncoded = true])
342364
Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
11
<?php
22

3-
// Go back to the previous page
4-
return redirect()->back();
5-
6-
// Go to specific URI
7-
return redirect()->to('/admin');
8-
9-
// Go to a named route
10-
return redirect()->route('named_route');
11-
12-
// Keep the old input values upon redirect so they can be used by the `old()` function
13-
return redirect()->back()->withInput();
14-
15-
// Set a flash message
16-
return redirect()->back()->with('foo', 'message');
17-
18-
// Copies all cookies from global response instance
19-
return redirect()->back()->withCookies();
20-
21-
// Copies all headers from the global response instance
22-
return redirect()->back()->withHeaders();
3+
// Go to specific URI path. "admin/home" is the URI path relative to baseURL.
4+
return redirect()->to('admin/home');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

3-
// Go to a named/reverse-routed URI
3+
// Go to a named/reverse-routed URI.
44
return redirect('named_route');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Go to a named route. "user_gallery" is the route name, not a URI path.
4+
return redirect()->route('user_gallery');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
// Go back to the previous page.
4+
return redirect()->back();
5+
6+
// Keep the old input values upon redirect so they can be used by the `old()` function.
7+
return redirect()->back()->withInput();
8+
9+
// Set a flash message.
10+
return redirect()->back()->with('foo', 'message');
11+
12+
// Copies all cookies from global response instance.
13+
return redirect()->back()->withCookies();
14+
15+
// Copies all headers from the global response instance.
16+
return redirect()->back()->withHeaders();

user_guide_src/source/installation/upgrade_4xx.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ Helpers
131131
will be :doc:`../helpers/filesystem_helper` in CI4.
132132
- `String Helper <https://www.codeigniter.com/userguide3/helpers/string_helper.html>`_ functions
133133
in CI3 are included in :doc:`../helpers/text_helper` in CI4.
134-
- In CI4, ``redirect()`` returns a ``RedirectResponse`` instance instead of redirecting and terminating script execution. You must return it.
134+
- In CI4, ``redirect()`` is completely changed from CI3's.
135135
- `redirect() Documentation CodeIgniter 3.X <https://codeigniter.com/userguide3/helpers/url_helper.html#redirect>`_
136136
- `redirect() Documentation CodeIgniter 4.X <../general/common_functions.html#redirect>`_
137+
- In CI4, ``redirect()`` returns a ``RedirectResponse`` instance instead of redirecting and terminating script execution. You must return it.
138+
- You need to change CI3's ``redirect('login/form')`` to ``return redirect()->to('login/form')``.
137139

138140
Hooks
139141
=====

0 commit comments

Comments
 (0)