Skip to content

dispatcher

jarnoux edited this page Jul 15, 2013 · 10 revisions

Description

The dispatcher is a middleware that calls appropriate controllers and views according to the given configuration. It supports early flush and composition with subviews and nested templates.

Configuration

Example

"dispatcher": {
    "plans": {
        "/": {
            "get": ["header", "index", "footer"]
        },
        "/hello": {
            "get": ["header", "arrayBody", "composedBody", "footer"]
        }
    },
    "details": {
        "arrayBody": ["body.top", "body.middle", "body.bottom"],
        "composedBody": {
            "top": "body.top",
            "middle": "body.middle",
            "bottom": "body.bottom"
        }
    },
    "headers": {
        "/": {
            "Content-Type": "text/html"
        },
        "/about": {
            "Content-Type": "text/html"
        }
    }
}

Members

  • plans is a mandatory object, and just like the routing configuration is indexed by route paths and by verb. Each path and verb is assigned a plan description (see below for the description and syntax of plans).
  • details is an optional object, it is indexed by plan name and describes the plans which names are used in the routes or the plans object.
  • headers is optional, and details what headers you want to send with the response for a specified route.

Plans

A plan is described as either an array or an object. This array or object can itself contain the names of other plans, or names of view & controller pairs.

Tip: If dispatcher cannot find a controller with a corresponding name, it will assume the view is just static markup and render the view with a default controller which passes nothing to the template.

Array Plan

If a plan is described by an array, the dispatcher will execute the listed subplans/subtemplates asynchronously and flush to the http response the results of the listed subplans/subtemplates in order as soon as they become available. It will guarantee the order of the output is the same as the array.

Object Plan

If a plan is described by an object, the dispatcher will execute the controller named after the plan and the listed subplans/subtemplates asynchronously. It will then inject the results of the subplans/subtemplates in the main controller's corresponding view at the placeholders given by the keys of the plan.

Performance Considerations

Describing a plan as an object will prevent an early flush of the plan. It is a good idea to be able to describe an output as the concatenation of several smaller outputs (array) rather than as the composition of them. However, it is not always practical and composition is often a great structure when early flushing is not crucially important.