hyperf/docs/en/response.md

158 lines
5.8 KiB
Markdown
Raw Normal View History

2020-08-11 00:08:36 +08:00
# Response
2019-03-30 20:55:41 +08:00
2020-08-11 00:08:36 +08:00
In Hyperf, you could get the response proxy object by injected `Hyperf\HttpServer\Contract\ResponseInterface` interface, by default, DI container will return an `Hyperf\HttpServer\Response` object, you could directly call all methods of `Psr\Http\Message\ResponseInterface` via this object.
2019-03-30 21:37:00 +08:00
2020-08-11 00:08:36 +08:00
> Note that the standard PSR-7 response object is an immutable object. The return value of all methods starts with `with` is a new object and will not modify the value of the original object.
2019-03-30 20:55:41 +08:00
2020-08-11 00:08:36 +08:00
## Return JSON
You could return a `Json` format content quickly by method `json($data)` of `Hyperf\HttpServer\Contract\ResponseInterface`, and also the `Content-Type` of response object will be set to `application/json`, `$data` accept an array or an object that implemented `Hyperf\Contract\Arrayable` interface.
2019-03-30 21:37:00 +08:00
2019-03-30 20:55:41 +08:00
```php
<?php
namespace App\Controller;
2019-06-23 14:57:42 +08:00
use Hyperf\HttpServer\Contract\ResponseInterface;
2019-03-30 20:55:41 +08:00
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
class IndexController
{
public function json(ResponseInterface $response): Psr7ResponseInterface
{
$data = [
'key' => 'value'
];
return $response->json($data);
}
}
```
2020-08-11 00:08:36 +08:00
## Return XML
2019-03-30 20:55:41 +08:00
You could return a `XML` format content quickly by method `xml($data)` of `Hyperf\HttpServer\Contract\ResponseInterface`, and also the `Content-Type` of response object will be set to `application/xml`, `$data` accept an array or an object that implemented `Hyperf\Contract\Xmlable` interface.
2019-03-30 21:37:00 +08:00
2019-03-30 20:55:41 +08:00
```php
<?php
namespace App\Controller;
2019-06-23 14:57:42 +08:00
use Hyperf\HttpServer\Contract\ResponseInterface;
2019-03-30 20:55:41 +08:00
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
class IndexController
{
public function xml(ResponseInterface $response): Psr7ResponseInterface
{
$data = [
'key' => 'value'
];
return $response->xml($data);
}
}
```
2020-08-11 00:08:36 +08:00
## Return the raw content
2019-03-30 20:55:41 +08:00
2020-08-11 00:08:36 +08:00
You could return the raw content quickly by method `raw($data)` of `Hyperf\HttpServer\Contract\ResponseInterface`, and also the `Content-Type` of response object will be set to `plain/text`, `$data` accept a string or an object that implemented `__toString()` method.
2019-03-30 21:37:00 +08:00
2019-03-30 20:55:41 +08:00
```php
<?php
namespace App\Controller;
2019-06-23 14:57:42 +08:00
use Hyperf\HttpServer\Contract\ResponseInterface;
2019-03-30 20:55:41 +08:00
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
class IndexController
{
public function raw(ResponseInterface $response): Psr7ResponseInterface
{
2019-07-02 01:51:24 +08:00
return $response->raw('Hello Hyperf.');
2019-03-30 20:55:41 +08:00
}
}
```
2020-08-11 00:08:36 +08:00
## Return view
2019-03-30 20:55:41 +08:00
2020-08-11 00:08:36 +08:00
Please refer to [View](zh-cn/view.md).
2019-03-30 20:55:41 +08:00
2020-08-11 00:08:36 +08:00
## Redirection
2019-03-30 20:55:41 +08:00
2020-08-11 00:08:36 +08:00
`Hyperf\HttpServer\Contract\ResponseInterface` provides `redirect(string $toUrl, int $status = 302, string $schema = 'http')` method to return an `Psr7ResponseInterface` object which has already setup redirection status.
2019-03-30 21:37:00 +08:00
`redirect`:
2019-03-30 21:37:00 +08:00
2020-08-11 00:08:36 +08:00
| Arguments | Type | Default Value | Comment |
|:------:|:------:|:------:|:--------------------------------------------------------------------------------------------------------------:|
| toUrl | string | null | If the argument does not starts with `http://` or `https://`, the corresponding URL will be automatically spliced according to the Host of the current server, and the splicing protocol according to the `$schema` argument |
| status | int | 302 | Status code of Response |
| schema | string | http | Effective when `$toUrl` does not starts with `http://` or `https://`, only `http` or `https` are available |
2019-03-30 21:37:00 +08:00
2019-03-30 20:55:41 +08:00
```php
<?php
namespace App\Controller;
2019-06-23 14:57:42 +08:00
use Hyperf\HttpServer\Contract\ResponseInterface;
2019-03-30 20:55:41 +08:00
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
class IndexController
{
public function redirect(ResponseInterface $response): Psr7ResponseInterface
{
2020-08-11 00:08:36 +08:00
// redirect() method will return an Psr\Http\Message\ResponseInterface object, needs to return the object.
2019-03-30 20:55:41 +08:00
return $response->redirect('/anotherUrl');
}
}
```
2020-08-11 00:08:36 +08:00
## Cookie
2019-03-30 21:37:00 +08:00
```php
<?php
namespace App\Controller;
2019-06-23 14:57:42 +08:00
use Hyperf\HttpServer\Contract\ResponseInterface;
2019-03-30 21:37:00 +08:00
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
2020-08-11 00:08:36 +08:00
use Hyperf\HttpMessage\Cookie\Cookie;
2019-03-30 21:37:00 +08:00
class IndexController
{
public function cookie(ResponseInterface $response): Psr7ResponseInterface
{
$cookie = new Cookie('key', 'value');
return $response->withCookie($cookie)->withContent('Hello Hyperf.');
}
}
```
2020-08-11 00:08:36 +08:00
## Gzip Compression
## Chunk
## File Download
`Hyperf\HttpServer\Contract\ResponseInterface` provides `download(string $file, string $name = '')` method to return an `Psr7ResponseInterface` object which already setup the file download status.
If the request contains `if-match` or `if-none-match` header, Hyperf will also compare it with the `ETag` according to the protocol standard, and if they match, it will return a response with a `304` status code.
`download`:
2019-03-30 21:37:00 +08:00
2020-08-11 00:08:36 +08:00
| Arguments | Type | Default Value | Comment |
|:----:|:------:|:------:|:-------------------------------------------------------------------:|
| file | string | null | To return to the absolute path of the downloaded file, use the `BASE_PATH` constant to locate the root directory of the project |
| name | string | null | The file name of the client download file, if it is empty, the original name of the downloaded file will be used |
```php
<?php
namespace App\Controller;
2019-03-30 21:37:00 +08:00
2020-08-11 00:08:36 +08:00
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
class IndexController
{
public function index(ResponseInterface $response): Psr7ResponseInterface
{
return $response->download(BASE_PATH . '/public/file.csv', 'filename.csv');
}
}
```