{
    "href": "/post/2015/05/26/radar-answering-questions-and-new-middleware/",
    "relId": "2015/05/26/radar-answering-questions-and-new-middleware",
    "title": "Radar: Answering Questions, and New Middleware",
    "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"
        },
        {
            "href": "/tag/radar/",
            "relId": "radar",
            "title": "Radar",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2015-05-26 13:23:32 UTC",
    "updated": [
        "2015-05-26 13:23:32 UTC"
    ],
    "html": "<p>Last week\u2019s announcement of <a href=\"https://github.com/radarphp/Radar.Project\">Radar</a>, an implementation of <a href=\"http://pmjones.io/adr\">Action-Domain-Responder</a> using <a href=\"http://www.php-fig.org/psr/psr-7/\">PSR-7</a>, was a resounding success. The feedback has been mostly positive, with some confusion and misunderstanding, and with one particular question being raised more than once.</p>\n<p>This <a href=\"http://www.reddit.com/r/PHP/comments/36hz5y/radar_a_psr7_actiondomainresponder_framework/\">Reddit thread</a> is typical of the various questions and comments I\u2019ve received so far:</p>\n<blockquote>\n<p>[\u201cADR\u201d is just] the hipster slang for \u2018c\u2019, \u2018m\u2019 and \u2018v\u2019.</p>\n</blockquote>\n<p>In other words, \u201cWhy ADR in the first place, and not just MVC?\u201d  This is answered by <a href=\"http://pmjones.io/adr\">the ADR white paper</a>, and again by some commentary <a href=\"http://www.reddit.com/r/PHP/comments/36hz5y/radar_a_psr7_actiondomainresponder_framework/crevqr6\">here</a>. In short, ADR is a refinement of \u201cweb\u201d MVC to more accurately describe how we actually work in a request/response environment. The renaming of the components is intended to break the association with \u201creal\u201d MVC. (Special thanks to \u201clordofworms\u201d for his patient and civil responses to the questioner.)</p>\n<blockquote>\n<p>Why [is the project called] \u201cRadar\u201d?</p>\n</blockquote>\n<p>As noted in the Reddit thread by others, \u201cRadar\u201d is a sound-alike from the ADR acronym. (An early version of the ADR paper was titled Request-Action-Domain-Response, the acronym for which lends itself even more to \u201cRadar\u201d, and I had that in mind too.)</p>\n<blockquote>\n<p>why use classes at all? If most of your classes contain a single __invoke method, could they not just be substituted with namespaced functions?</p>\n</blockquote>\n<p>The questioner was <a href=\"https://twitter.com/taylorotwell/status/600680897550098432\">answered quite thoroughly on Twitter</a>. I will add only that there\u2019s nothing about ADR that says you <em>must</em> use classes. If you can implement ADR using all functions all the time, go for it. ADR is a pattern, not an implementation; Radar is an implementation, not a pattern.</p>\n<p>Finally, we have this very good criticism referring to the middleware implementation:</p>\n<blockquote>\n<p>Passing arguments by reference will confuse users.</p>\n</blockquote>\n<p>Variations on this theme came up elsewhere as well. As a result, I have re-worked the middleware implementation completely.</p>\n<p>Previously, Radar used a \u201cfilter\u201d style of middleware, where an iterator calls each middleware handler in turn at different points in the execution path. This is the style used by <a href=\"http://slimframework.com\">Slim 2</a> and <a href=\"http://silex.sensiolabs.org\">Silex</a>, and works well with globally mutable request and response objects.</p>\n<p>However, PSR-7 requests and response are immutable. I have not previously used immutables very much, and I tried to subvert the immutability by allowing middleware to use reference parameters. The references would make the request and response objects appear globally mutable, even though the objects were in fact immutable.</p>\n<p>The critics who noted that this was potentially confusing, as well as a subversion of the immutable intent, turned out to be worth listening to. I experimented a bit with a wrapper-style (or chain-style) of middleware, and I\u2019m much happier with it.</p>\n<p>In the new implementation, each middleware calls the next one in turn, instead of an external iterator looping over them. (There is a Dispatcher object that \u201cholds\u201d the queue of handlers, and that it what gets passed to each handler for its <code>$next</code> call.) I got the idea from reading MWOP\u2019s <a href=\"https://github.com/phly/conduit\">Conduit</a> code, although this is greatly pared down from that. You can read more about the new implementation <a href=\"https://github.com/radarphp/Radar.Project/blob/1.x/docs/middleware.md\">here</a>.</p>\n<hr>\n<p class=\"reddit-links\">Read the Reddit discussion about this post <a href=\"https://www.reddit.com/r/PHP/comments/37bpwz/radar_answering_questions_and_new_middleware/\">here</a>.</p>\n"
}
