HTTP Factories (PSR-17)


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

    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 objects.

    The Phalcon\Http\Message\ResponseFactory can be used to create 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 can be used to create Phalcon\Http\Message\ServerRequest objects.

    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.

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

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

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

    UploadedFileFactory

    The Phalcon\Http\Message\UploadedFileFactory can be used to create 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. );

    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 . Otherwise they can be set to null.