psr-7 - http message interfaces

Click here to load reader

Post on 13-Feb-2017

1.238 views

Category:

Software

2 download

Embed Size (px)

TRANSCRIPT

  • tekst

    PSR-7 - HTTP message interfacesPawe Skotnicki

  • About me

    PHP developer @ The Software House

    PHPers Silesia co-organizer

    BDD/DDD enthusiast

  • PHP-FIG ?

  • PHP Framework Interop Group ?

  • PHP Standard Recommendation ?

  • PSR ?

  • PHP-FIG

    PHP Framework Interop Group

    40+ members

    PSR

  • PSR-0 Autoloading Standard

  • PSR-1 Basic Coding Standard

  • PSR-2 Coding Style Guide

  • PSR-3 Logger Interface

  • PSR-4 Autoloading Standard

  • PSR-5 PHPDoc Standard(Draft)

  • PSR-6 Caching Interface(Review)

  • PSR-7 HTTP message interfaces

  • HTTP request

    POST /path HTTP/1.1

    Host: example.com

    foo=bar&baz=bat

  • HTTP response

    HTTP/1.1 200 OK

    Content-Type: text/plain

    This is the response body

  • PSR-7

    http://www.php-fig.org/psr/psr-7/

    https://github.com/php-fig/http-message

    composer require psr/http-message

    namespace Psr\Http\Message

    https://github.com/php-fig/http-message

  • Psr\Http\MessageMessageInterface

    ResponseInterface

    RequestInterface

    ServerRequestInterface

    UriInterface

    StreamInterface

    UploadedFileInterface

  • MessageInterface

    immutable value objects

    protocol version

    headers

    body

  • HTTP Message Headers$message->getHeader('Accept'); //array

    $message->getHeaderLine('Accept'); //string

    $message->hasHeader('Accept'); //boolean

    /*

    ['Header => ['value1', 'value2']]

    */

    $message->getHeaders();

  • HTTP Message Headers$message = $message->withHeader('X-Foo', 'bar');

    $message = $message->withAddedHeader(

    'X-Foo',

    'baz'

    );

    $message = $message->withoutHeader('X-Foo');

  • HTTP Message Body

    StreamInterface

    may be mutable !

  • ResponseInterface

    $response->getStatusCode();

    $response->getReasonPhrase();

    $response = $response->withStatus(200, 'OK');

  • RequestInterface

    method

    request-target

    uri

    UriInterface

  • request-target

    origin-form

    absolute-form

    authority-form

    asterisk-form

  • RequestInterface

    Method

    RequestTarget

    Uri

  • ServerRequestInterfacegetServerParams ($_SERVER)

    CookieParams ($_COOKIE)

    QueryParams ($_GET)

    UploadedFiles ($_FILES)

    ParsedBody ($_POST)

    attributes

  • Uploaded Files

    UploadedFileInterface

    getStream() // StreamInterface

    moveTo()

    size, error, client filename, client media type

    getUploadedFiles() - normalized structure

  • Single file upload

  • $_FILES[

    'avatar' => [

    'tmp_name' => 'phpUxcOty',

    'name' => 'my-avatar.png',

    'size' => 90996,

    'type' => 'image/png',

    'error' => 0,

    ],

    ]

  • getUploadedFiles()

    [

    'avatar' => /* UploadedFileInterface instance */

    ]

  • Multiple files upload

  • $_FILES[

    'my-form' => [

    'avatars' => [

    'tmp_name' => [

    0 => '...',

    1 => '...',

    ],

    'name' => [],

    'size' => [],

    'type' => [],

    'error' => [],

    ],

    ],

    ]

  • getUploadedFiles()

    [

    'my-form' => [

    'avatars' => [

    0 => /* UploadedFileInterface instance */,

    1 => /* UploadedFileInterface instance */,

    ],

    ],

    ]

  • Use cases

    Clients

    unified request and response interfaces

    Middleware

    Frameworks

  • Who ?

    Guzzle, PHP HTTP client

    Zend Diactoros, PSR HTTP Message implementation

    PSR HTTP Message Bridge (Symfony 2)

    Relay, PSR-7 middleware dispatcher

    Aura Router

  • Questions?

  • Thank you

View more