HTTP Factories (PSR-17)


PSR-17 HTTP Factories - 图1

Overview

Phalcon\Http\Message\RequestFactory, Phalcon\Http\Message\ResponseFactory, Phalcon\Http\Message\ServerRequestFactory, Phalcon\Http\Message\StreamFactory, Phalcon\Http\Message\UploadedFileFactory, Phalcon\Http\Message\UriFactory are the factories implemented of the PSR-17 HTTP messaging interface factories as defined by PHP-FIG.

PSR-17 HTTP Factories - 图2

These components aid in creating HTTP objects as defined by the PSR-7 standard.

RequestFactory

The Phalcon\Http\Message\RequestFactory can be used to create Phalcon\Http\Message\Request objects.

  1. <?php
  2. use Phalcon\Http\Message\RequestFactory;
  3. $factory = new RequestFactory();
  4. $stream = $factory->createRequest(
  5. 'GET',
  6. 'https://api.phalcon.io/companies/1'
  7. );

The createRequest() method accepts a string as the method (GET, POST etc.) and the URI and returns back the request object.

ResponseFactory

The Phalcon\Http\Message\ResponseFactory can be used to create Phalcon\Http\Message\Response objects.

  1. <?php
  2. use Phalcon\Http\Message\ResponseFactory;
  3. $factory = new ResponseFactory();
  4. $stream = $factory->createResponse(200, 'OK');

The createResponse() method accepts an integer which is the response status as well as a string, representing the reason phrase. If no reason is specified, the component will use the default ones as suggested by the HTTP RFCs.

ServerRequestFactory

The Phalcon\Http\Message\ServerRequestFactory can be used to create Phalcon\Http\Message\ServerRequest objects.

  1. <?php
  2. use Phalcon\Http\Message\ServerRequestFactory;
  3. $factory = new ServerRequestFactory();
  4. $request = $factory->createServerRequest(
  5. 'GET',
  6. 'https://api.phalcon.io/companies/1',
  7. [
  8. 'param' => 'value'
  9. ]
  10. );

The createServerRequest() creates the new object using a method (GET, POST etc.), a URI and optionally an array of SAPI parameters with which to seed the generated request instance.

In addition to the createServerRequest() the factory offers the load() method as a helper to create a request by populating it from the superglobals.

  1. <?php
  2. use Phalcon\Http\Message\ServerRequestFactory;
  3. $factory = new ServerRequestFactory();
  4. $request = $factory->load(
  5. $_SERVER,
  6. $_GET,
  7. $_POST,
  8. $_COOKIE,
  9. $_FILES
  10. );

If If any argument is not supplied, the corresponding superglobal will be used.

StreamFactory

The Phalcon\Http\Message\StreamFactory can be used to create Phalcon\Http\Message\Stream objects.

  1. <?php
  2. use Phalcon\Http\Message\StreamFactory;
  3. $factory = new StreamFactory();
  4. $stream = $factory->createStream('stream contents');

UploadedFileFactory

The Phalcon\Http\Message\UploadedFileFactory can be used to create Phalcon\Http\Message\UploadedFile objects.

  1. <?php
  2. use Phalcon\Http\Message\StreamFactory;
  3. use Phalcon\Http\Message\UploadedFileFactory;
  4. $factory = new UploadedFileFactory();
  5. $streamFactory = new StreamFactory();
  6. $stream = $streamFactory->createStream('stream contents');
  7. $size = 12345;
  8. $error = 0;
  9. $clientFilename = null;
  10. $clientMediaType = null;
  11. $file = $factory->createUploadedFile(
  12. $stream,
  13. $size,
  14. $error,
  15. $clientFilename,
  16. $clientMediaType
  17. );

If a size is not provided it will be determined by checking the size of the stream. The $error is the PHP file upload error, It defaults to 0. If provided by the client, you can use the clientFilename and clientMediaType. Otherwise they can be set to null.

UriFactory

The Phalcon\Http\Message\UriFactory can be used to create Phalcon\Http\Message\Uri objects.

  1. <?php
  2. use Phalcon\Http\Message\UriFactory;
  3. $factory = new UriFactory();
  4. $uri = $factory->createUri('https://api.phalcon.io/companies/1');