Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit cd5a24f

Browse files
committed
Documented RedirectResponse
1 parent c261f5e commit cd5a24f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

doc/book/custom-responses.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Some standard use cases, however, make this un-wieldy:
1717
- Returning a response containing JSON; in this case, you likely want to provide the data to
1818
seriazlize to JSON, not a stream containing serialized JSON.
1919
- Returning a response with no content; in this case, you don't want to bother with the body at all.
20+
- Returning a redirect response; in this case, you likely just want to specify the target for the
21+
`Location` header, and optionally the status code.
2022

2123
Starting with version 1.1, Diactoros offers several custom response types and factories for
2224
simplifying these common tasks.
@@ -95,6 +97,34 @@ $response = new EmptyResponse(201, [
9597
$response = ( new EmptyResponse(201) )->withHeader('Location', $url);
9698
```
9799

100+
## Redirects
101+
102+
`Zend\Diactoros\Response\RedirectResponse` is a `Zend\Diactoros\Response` extension for producing
103+
redirect responses. The only required argument is a URI, which may be provided as either a string or
104+
`Psr\Http\Message\UriInterface` instance. By default, the status 302 is used, and no other headers
105+
are produced; you may alter these via the additional optional arguments:
106+
107+
```php
108+
class RedirectResponse extends Response
109+
{
110+
public function __construct($uri, $status = 302, array $headers = []);
111+
}
112+
```
113+
114+
Typical usage is:
115+
116+
```php
117+
// 302 redirect:
118+
$response = new RedirectResponse('/user/login');
119+
120+
// 301 redirect:
121+
$response = new RedirectResponse('/user/login', 301);
122+
123+
// using a URI instance (e.g., by altering the request URI instance)
124+
$uri = $request->getUri();
125+
$response = new RedirectResponse($uri->withPath('/login'));
126+
```
127+
98128
## Creating custom responses
99129

100130
PHP allows constructor overloading. What this means is that constructors of extending classes can

0 commit comments

Comments
 (0)