Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
172 lines (126 sloc) 3.75 KB

Request

HTTP request contains 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!"
}

In the above method is POST, URI is /contact. Additional headers are specifying host, preferred language and encoding. The body could be anything. In this case it is 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 is one of the:

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD
  • PATCH
  • OPTIONS

URI

An URI has:

  • Scheme (http, https)
  • Host (yiiframework.com)
  • Port (80, 443)
  • Path (/posts/1)
  • Query string (page=1&sort=+id)
  • Fragment (#anchor)

An object of UriInterface could be obtained from request like the following:

$uri = $request->getUri();

Then various details could be obtained 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');

Alternatively value could be obtained as a comma-separated string instead of an array. That is especially handy if header contains a single value:

if ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') {
    // this is AJAX request
}

To check if a header present in the request:

if ($request->hasHeader('Accept-Encoding')) {
    // ...
}

Body

There are two method 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.

Alternatively, parsed body could be obtained:

$bodyParameters = $request->getParsedBody();

Parsing depends on PSR-7 implementation and may require a 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 $handler): Response
    {
        $contentType = $request->getHeaderLine('Content-Type');

        if (strpos($contentType, 'application/json') !== false) {
            $body = $request->getBody();
            $parsedBody = $this->parse($body);
            $request = $request->withParsedBody($parsedBody);
            
        }

        return $handler->handle($request);
    }
}

File uploads

Uploaded files that were 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. These attributes could be obtained with getAttribute(). For example, router is setting matched route parameters as same-named attributes.

You can’t perform that action at this time.