Request & amp; response

Swoft request and response implemented in PSR 7

The request and response object exists in each HTTP request, which is referred to here as Request Swoft\Http\Message\Request Response Swoft\Http\Message\Response .

Attention! Depending on the immutability of the PSR-7 object (immutable), all with* methods are cloned objects and then returned, having to receive new objects for further processing, or using chained calls

Basic method

The PSR-7 interface provides these common methods for request and response objects:

  • withProtocolVersion($version)
  • withHeader($name, $value)
  • withAddedHeader($name, $value)
  • withoutHeader($name)
  • withBody(StreamInterface $body)

The PSR-7 interface provides these methods for the request object:

  • withMethod(string $method)
  • withUri(UriInterface $uri, $preserveHost = false)
  • withCookieParams(array $cookies)
  • withQueryParams(array $query)
  • withUploadedFiles(array $uploadedFiles)
  • withParsedBody($data)
  • withAttribute($name, $value)
  • withoutAttribute($name)

The PSR-7 interface provides these methods for response objects:

  • withStatus($code, $reasonPhrase = '')

For more information, refer to PSR-7 and view swoft/http-message the specific implementation classes

Request Object

How to get

  • Injection through the controller Action parameter
  • Get through the request contextSwoft\Context\Context::mustGet()->getRequest()

Request Action Method

$request = \Swoft\Context\Context::mustGet()->getRequest();
$method = $request->getMethod();

The requested URI

Each HTTP request has a URI that identifies the requested application resource. There are several parts to the HTTP request URI:

  • Scheme (e.g. http or https )
  • Host (e.g. example.com )
  • Port (e.g. 80 or 443 )
  • Path (e.g. /users/1 )
  • Query string (e.g. sort=created&dir=asc )

You can getUri() get the PSR-7 URI object by requesting the object's method:

$uri = $request->getUri();

The URI of the PSR-7 request object is itself an object that provides the following methods to check the URL portion of the HTTP request:

  • getScheme()
  • getAuthority()
  • getUserInfo()
  • getHost()
  • getPort()
  • getPath()
  • getQuery()(E.g. a=1&b=2 )
  • getFragment()

Request Headers

All the Headers.

$headers = $request->getHeaders();

foreach ($headers as $name => $values) {
    echo $name . ": " . implode(", ", $values).PHP_EOL;
}

The specified Header

  • The return value is array
$headerValueArray = $request->getHeader('host');
print_r($headerValueArray);
  • The return value is a string
$host = $request->getHeaderLine("host");
print_r($host);

Some auxiliary methods

  • Xhr
if ($request->isAjax()) {
    // Do something
}
if ($request->isXmlHttpRequest()) {
    // Do something
}
  • GET
if ($request->isGet()) {
    // Do something
}
  • POST
if ($request->isPost()) {
    // Do something
}
  • PUT
if ($request->isPut()) {
    // Do something
}
  • DELETE
if ($request->isDelete()) {
    // Do something
}
  • PATCH
if ($request->isPatch()) {
    // Do something
}
  • Content-Type
$contentType = $request->getContentType();

Request Data Acquisition

Get Data

$data = $request->query();
$some = $request->query('key', 'default value')
$data = $request->get();
$some = $request->get('key','default value');

POST data

$data = $request->post();
$some = $request->post('key', 'default value')

Get the POST data

$data = $request->input();
$some = $request->input('key', 'default value')

JSON data

Valid only if Content-Type the application/json time is

$data = $request->json();
$some = $request->json('key', 'default value')

RAW data

$data = $request->raw();

SERVER data

$data = $request->getServerParams();
$some = $request->server('key', 'default value')

Response Object

How to get

  • Injection through the controller Action parameter
  • Get through the request contextSwoft\Context\Context::mustGet()->getResponse()

Common methods

Output Status Code

$response = \Swoft\Context\Context::mustGet()->Response();
return $response->withStatus(404);

Output string Contents

return $response->withContent("Hello Swoft2.0");

Output array

$data = ['name'=>'Swoft2.0'];
$response->withData($data);

Output header information

return $response->withHeader("name","Swoft2.0");

Redirection

return $response->redirect("http://www.swoft.org",302);

File downloads

return $response->file(\alias('@runtime/1.zip'),"application/octet-stream");
/docs/2.x/en/http-server/request-response.html
progress-bar