A stratigility component for dispatch action based on configuration file
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



Build Status

This component is a proposal for a PHP middleware dispatcher compliant with PSR-7 standard. This components has been developed with zend-stratigility in mind, but can be used in any PSR-7 middleware application.

For the routing part we used a pluggable adapter architecture using a simple RouterInterface

We provide a default router adapter using the Aura.Router library.

You can write your adapter implementing the Zend\Stratigility\Dispatch\Router\RouterInterface.


You can install this component in your PHP application using composer. You need to add the following require in your composer.json file.

"ezimuel/zend-stratigility-dispatch" : "dev-master"

Basic usage

The main goal of this component is to dispatch a PHP middleware application using a simple configuration array, like the following:

return [
    'router' => [
        'adapter' => 'Zend\Stratigility\Dispatch\Router\Aura'
    'routes' => [
        // example of action class without dependencies
        'home' => [
            'url'    => '/',
            'action' => 'App/Action/Home'
        // example of factory action class
        'example' => [
            'url'    => '/example',
            'action' => 'App/Action/ExampleFactory::factory'
        // example of action class with dependencies using an anonymous function
        'page' => [
            'url' => '/page',
            'action' => function($request, $response, $next) {
                $bar  = new App/Model/Bar();
                $page = new App/Action/Page($bar);
                return $page->action($request, $response, $next);
        // example of action class using optional parameter in the URL
        // the syntax of the URL and the tokens depend on the router adapter (Aura in this case)
        'search' => [
            'url'    => '/search{/query}',
            'tokens' => [ 'query' => '([^/]+)?' ],
            'action' => 'App/Search'

The router key specifies the routing adapter to use, the default is the Aura.Router. All the routes are specified in the routes array. For each route you need to specify at least a url and an action that can be a callable, a class name, a static function name, etc.

To use the dispatcher you can call the MiddlewareDispatch::factory that create an instance of Dispatcher. With this istance in place you can just attach it to a zend-stratigility application using the MiddlewarePipe component:

use Zend\Stratigility\MiddlewarePipe;
use Zend\Diactoros\Server;
use Zend\Stratigility\Dispatch\MiddlewareDispatch;

require '../vendor/autoload.php';

$app = new MiddlewarePipe();
$app->pipe('/', MiddlewareDispatch::factory(require '../config/route.php'));

$server = Server::createServer($app, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);

where the previous configuration file is stored in ../config/route.php file.

Using a Container

If you want you can consume actions from a DI Container. We used the ContainerInterface of the Container Interoperability project. This is a proposal for a PSR standard.

To use a Container you need to inject in the dispatcher using the setContainer() method.

Here is reported an example:

// here the code to create a $container (implementing the Interop\Container\ContainerInterface)

$app = new MiddlewarePipe();

$dispatcher = MiddlewareDispatch::factory(require '../config/route.php');

$app->pipe('/', $dispatcher);

$server = Server::createServer($app, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);


This component is still in development, DO NOT use in production!