{
    "href": "/post/2017/01/03/rfc-serverrequest-and-serverresponse/",
    "relId": "2017/01/03/rfc-serverrequest-and-serverresponse",
    "title": "RFC: ServerRequest and ServerResponse",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2017-01-03 15:27:55 UTC",
    "updated": [
        "2017-01-03 15:27:55 UTC"
    ],
    "html": "<p>The RFC is at <a href=\"https://wiki.php.net/rfc/request_response\">https://wiki.php.net/rfc/request_response</a>.</p>\n<p>The message opening discussion on Internals is at <a href=\"http://news.php.net/php.internals/97461\">http://news.php.net/php.internals/97461</a>.</p>\n<p>The extension itself is available at <a href=\"https://pecl.php.net/package/request\">https://pecl.php.net/package/request</a>, with documentation at <a href=\"https://gitlab.com/pmjones/ext-request\">https://gitlab.com/pmjones/ext-request</a>.</p>\n<p>(Many thanks to John Boehr for doing the actual heavy lifting of writing the C code.)</p>\n<hr>\n<p>Nearly every PHP framework and library-collection since 2000 has had classes to encapsulate the \u201crequest\u201d and \u201cresponse\u201d elements of a PHP application. A handful of examples include:</p>\n<ul>\n<li>\n<a href=\"https://github.com/auraphp/Aura.Web/blob/2.x/src/Request.php\">Aura\\Web\\Request</a> and <a href=\"https://github.com/auraphp/Aura.Web/blob/2.x/src/Response.php\">Response</a>.</li>\n<li>\n<a href=\"https://github.com/cakephp/cakephp/blob/master/src/Network/Request.php\">Cake\\Network\\Request</a> and <a href=\"https://github.com/cakephp/cakephp/blob/master/src/Network/Response.php\">Response</a>.</li>\n<li>Code Igniter <a href=\"https://github.com/bcit-ci/CodeIgniter/blob/develop/system/core/Input.php\">Input</a> (request) and <a href=\"https://github.com/bcit-ci/CodeIgniter/blob/develop/system/core/Output.php\">Output</a> (response).</li>\n<li>\n<a href=\"https://github.com/horde/horde/blob/master/framework/Controller/lib/Horde/Controller/Request/Http.php\">Horde_Controller_Request_Http</a> and <a href=\"https://github.com/horde/horde/blob/master/framework/Controller/lib/Horde/Controller/Response.php\">Horde_Controller_Response</a>.</li>\n<li>\n<a href=\"https://github.com/joomla-framework/input/blob/master/src/Input.php\">Joomla\\Input\\Input</a> (request) and <a href=\"https://github.com/joomla-framework/application/blob/master/src/AbstractWebApplication.php\">Joomla\\Application\\AbstractWebApplication</a> (response handling).</li>\n<li>\n<a href=\"https://github.com/UnionOfRAD/lithium/blob/master/action/Request.php\">lithium\\action\\Request</a> and <a href=\"https://github.com/UnionOfRAD/lithium/blob/master/action/Response.php\">Response</a>.</li>\n<li>MediaWiki <a href=\"https://github.com/wikimedia/mediawiki/blob/master/includes/WebRequest.php\">WebRequest</a> and <a href=\"https://github.com/wikimedia/mediawiki/blob/master/includes/WebResponse.php\">WebResponse</a> (though the latter does not encapsulate content).</li>\n<li>\n<a href=\"https://docs.phalconphp.com/en/latest/api/Phalcon_Http_Request.html\">Phalcon\\Http\\Request</a> and <a href=\"https://docs.phalconphp.com/en/latest/api/Phalcon_Http_Response.html\">Response</a>.</li>\n<li>\n<a href=\"https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php\">Symfony\\Component\\HttpFoundation\\Request</a> and <a href=\"https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Response.php\">Response</a> (Drupal and Laravel extend from these).</li>\n<li>\n<a href=\"https://secure.php.net/manual/en/class.yaf-request-http.php\">Yaf_Request_Http</a> and <a href=\"https://secure.php.net/manual/en/class.yaf-response-abstract.php\">Response</a>.</li>\n<li>\n<a href=\"https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php\">yii\\web\\Request</a> and <a href=\"https://github.com/yiisoft/yii2/blob/master/framework/web/Response.php\">Response</a>.</li>\n<li>\n<a href=\"https://github.com/zendframework/zend-http/blob/master/src/PhpEnvironment/Request.php\">Zend\\PhpEnvironment\\Request</a> and <a href=\"https://github.com/zendframework/zend-http/blob/master/src/PhpEnvironment/Response.php\">Response</a>\n</li>\n</ul>\n<p>There are many others. They all do essentially the same things:</p>\n<ul>\n<li>\n<p>Copy  the <code>$_GET</code>, <code>$_POST</code>, <code>$_SERVER</code>, etc. superglobals into a \u201crequest\u201d object. Some make them available through a method that standardizes the logic to get a default value when a key is not present, a la <code>return (isset($_GET[$key]) ? $_GET[$key] : $defaultValue)</code>.</p>\n</li>\n<li>\n<p>Add convenience methods to the \u201crequest\u201d object so that you can determine the HTTP method, the values of various headers, and so on.</p>\n</li>\n<li>\n<p>The \u201cresponse\u201d object is a place to hold headers, cookies, status, and content, so they can all be inspected and modified before sending, and to make testing easier. (This is because the <code>header()</code>, <code>setcookie()</code>, etc. functions in PHP are not especially amenable to inspection, modification, and testing \u2013 at least, not without being wrapped somehow.)</p>\n</li>\n<li>\n<p>The \u201cresponse\u201d object often has some convenience methods to send JSON content, send files for download, and so on.</p>\n</li>\n</ul>\n<p>Why do framework and library-collection authors write these request and response objects? Because PHP, even though it is a web-centric programming language, and even though it provides all sorts of classes for all sorts of functionality, it has never had classes for server-side requests and responses. This RFC helps to improve this situation in PHP 7 and later.</p>\n<p class=\"reddit-links\">Read the Reddit discussion about this post <a href=\"https://www.reddit.com/r/PHP/comments/5lshko/rfc_serverrequest_and_serverresponse/\">here</a>.</p>\n"
}
