marcojanssen/silex-routing-service-provider

1.5.0 2016-02-08 08:27 UTC

README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

RoutingServiceProvider is a silex provider for easily adding routes

Features

  • Register providers through configuration
  • Register multiple providers with the provider
  • Register a single provider with the provider

Installing

Via Composer

composer require marcojanssen/silex-routing-service-provider

Options

Each route is required to have the following parameters:

  • pattern (string)
  • controller (string)
  • method - get, put, post, delete, options, head (array|string)

Optionally, you can set a route name (for named routes). The key of the $route-array will be used as the route name or you can set it like this:

$routes = array(
    'foo' => array(
        //'name' => 'foo', --> you can omit the name if a key is set
        'pattern' => '/foo',
        'controller' => 'Foo\Controller\FooController::fooAction',
        'method' => array('get', 'post', 'put', 'delete', 'options', 'head')
    )
);

Optionally the following parameters can also be added:

value (array)

You can add default values for any route variable: http://silex.sensiolabs.org/doc/usage.html#default-values

$value = array('name' => 'value')

assert (array)

You can add requirements: http://silex.sensiolabs.org/doc/usage.html#requirements

$assert = array('id' => '^[\d]+$')

convert (array)

You can add route variable converters: http://silex.sensiolabs.org/doc/usage.html#route-variable-converters. Before you can use the route variable converter, you need to add it as a service.

$after = array('convert' => function() {})

before (string|array)

Add a before-middleware: http://silex.sensiolabs.org/doc/middlewares.html#before-middleware

$before = array('before' => function() {})

after (string|array)

Add an after-middleware: http://silex.sensiolabs.org/doc/middlewares.html#after-middleware

$after = array('after' => function() {})

Usage

Adding a single route

index.php

use Silex\Application;
use MJanssen\Provider\RoutingServiceProvider;

$app = new Application();
$routingServiceProvider = new RoutingServiceProvider();

$route = array(
    'name' => 'foo',
    'pattern' => '/foo',
    'controller' => 'Foo\Controller\FooController::fooAction',
    'method' => array('get', 'post', 'put', 'delete', 'options', 'head')
);

$routingServiceProvider->addRoute($app, $route);

Adding multiple routes

index.php

use Silex\Application;
use MJanssen\Provider\RoutingServiceProvider;

$app = new Application();
$routingServiceProvider = new RoutingServiceProvider();

$routes = array(
    'foo' => array(
        //'name' => 'foo', --> you can omit the route name if a key is set
        'pattern' => '/foo',
        'controller' => 'Foo\Controller\FooController::fooAction',
        'method' => array('get', 'post', 'put', 'delete', 'options', 'head')
    ),
    'baz' => array(
        //'name' => 'baz', --> you can omit the route name if a key is set
        'pattern' => '/baz',
        'controller' => 'Baz\Controller\BazController::bazAction',
        'method' => 'get'
    )
);

$routingServiceProvider->addRoutes($app, $route);

Adding before/after middleware

To add controller middleware you can use the 'after' and 'before' key of the route configuration. The 'before' key is used to run the middleware code before the controller logic is executed, after execution of the controller logic. Below is an example using a middleware class and how to configure this in the route config. Instead of using a middleware class you can also use a regular callback.

Note Be aware that currently there is only support for php.

Example middleware class:

class MiddleWare {

    public function __invoke(Request $request, Application $app)
    {
        //do stuff
        $x = 1;
    }
}

Using the middleware class in the route configuration

index.php

use Silex\Application;
use MJanssen\Provider\RoutingServiceProvider;

$app = new Application();
$routingServiceProvider = new RoutingServiceProvider();

$routes = array(
    'foo' => array(
        //'name' => 'foo', --> you can omit the route name if a key is set
        'pattern' => '/foo',
        'controller' => 'Foo\Controller\FooController::fooAction',
        'method' => array('get'),
        // this is where it all happens!
        'before' => array(new MiddleWare())
    )
);
$routingServiceProvider->addRoutes($app, $route);

Adding a route middleware class via yml

You can add middleware classes via yml-/xml-configuration. Example:

routes.yaml

config.routes:
    home:
        name: 'home'
        pattern: /
        method: [ 'get', 'post' ]
        controller: 'Foo\Controllers\FooController::getAction'
        before: 'Foo\Middleware\FooController::before'
        after: 'Foo\Middleware\FooController::after'

The methods' interface need to match the Silex specification. Further information about route middleware classses: http://silex.sensiolabs.org/doc/middlewares.html#route-middlewares.

ATTENTION: Unfortunately with this way, you cannot use the ´__invoke´ method described above.

Registering providers with configuration

For this example the ConfigServiceProvider is used to read the yml file. The RoutingServiceProvider picks the stored configuration through the node config.routing as in $app['config.routing'] by default. If you want to set a different key, add it as parameter when instantiating the RoutingServiceProvider ***Note: The key of the array will also be used as the ´route´, so you can omit ´route.

routes.yaml

config.routes:
    home:
        name: 'home'
        pattern: /
        method: [ 'get', 'post' ]
        controller: 'Foo\Controllers\FooController::getAction'

routes.php

return array(
    'custom.routing.key' => array(
        array(
            'pattern' => '/foo/{id}',
            'controller' => 'Foo\Controllers\FooController::getAction',
            'method' => 'get',
            'assert' => array(
                'id' => '^[\d]+$'
            ),
            'value' => array(
                'value1' => 'foo',
                'value2' => 'baz'
            )
        )
    )
);

index.php

use Silex\Application;
use Igorw\Silex\ConfigServiceProvider;
use MJanssen\Provider\RoutingServiceProvider;

$app = new Application();

//Set all routes
$app->register(
    new ConfigServiceProvider(__DIR__."/../app/config/routes.php")
);

//Add all routes
$app->register(new RoutingServiceProvider('custom.routing.key'));

Note: It's recommended to use php instead of yml/xml/etc, because it can be opcached. Otherwise you have to implement a caching mechanism yourself.

Todo

  • convert: route variable converters need to be services. We need a way to add them as a valid callback like MyNamespace\MyClass::converter