Request
HTTP request has a method, URI, a set of headers and a body:
POST /contact HTTP/1.1
Host: example.org
Accept-Language: en-us
Accept-Encoding: gzip, deflate
{
"subject": "Hello",
"body": "Hello there, we need to build Yii application together!"
}
The method is POST
, URI is /contact
. Extra headers are specifying host, preferred language and encoding. The body could be anything. In this case, it's JSON payload.
Yii uses PSR-7 ServerRequest
as request representation. The object is available in controller actions and other types of middleware:
public function view(ServerRequestInterface $request): ResponseInterface
{
// ...
}
Method
The method could be obtained from request object:
$method = $request->getMethod();
Usually it's one of the:
- GET
- POST
- PUT
- DELETE
- HEAD
- PATCH
- OPTIONS
In case you want to make sure the request method is of a certain type, there is a special class with method names:
use Yiisoft\Http\Method;
if ($request->getMethod() === Method::POST) {
// method is POST
}
URI
A URI has:
- Scheme (
http
,https
) - Host (
yiiframework.com
) - Port (
80
,443
) - Path (
/posts/1
) - Query string (
page=1&sort=+id
) - Fragment (
#anchor
)
You can obtain UriInterface
from request like the following:
$uri = $request->getUri();
Then you can get various details from its methods:
getScheme()
getAuthority()
getUserInfo()
getHost()
getPort()
getPath()
getQuery()
getFragment()
Headers
There are various methods to inspect request headers. To get all headers as an array:
$headers = $request->getHeaders();
foreach ($headers as $name => $values) {
// ...
}
To obtain a single header:
$values = $request->getHeader('Accept-Encoding');
Also, you could obtain value as a comma-separated string instead of an array. That's especially handy if a header has a single value:
if ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') {
// This is AJAX request made with jQuery.
// Note that header presence and name may vary depending on the library used.
}
To check if a header is present in the request:
if ($request->hasHeader('Accept-Encoding')) {
// ...
}
Body
There are two methods to obtain body contents. First is getting body as is without parsing:
$body = $request->getBody();
The $body
would be an instance of Psr\Http\Message\StreamInterface
.
Also, you could obtain a parsed body:
$bodyParameters = $request->getParsedBody();
Parsing depends on PSR-7 implementation and may require middleware for custom body formats.
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
class JsonBodyParserMiddleware implements MiddlewareInterface
{
public function process(Request $request, RequestHandler $next): Response
{
$contentType = $request->getHeaderLine('Content-Type');
if (strpos($contentType, 'application/json') !== false) {
$body = $request->getBody();
$parsedBody = $this->parse($body);
$request = $request->withParsedBody($parsedBody);
}
return $next->handle($request);
}
}
File uploads
Uploaded files that user submitted from a form with enctype
attribute equals to multipart/form-data
are handled via special request method:
$files = $request->getUploadedFiles();
foreach ($files as $file) {
if ($file->getError() === UPLOAD_ERR_OK) {
$file->moveTo('path/to/uploads/new_filename.ext');
}
}
Attributes
Application middleware may set custom request attributes using withAttribute()
method. You can obtain these attributes with getAttribute()
.