marcojanssen/silex-routing-service-provider

Silex provider for adding routes

Installs: 5 082

Dependents: 1

Stars: 5

Watchers: 2

Forks: 7

Open Issues: 2

Language: PHP

1.3.0 2015-04-21 20:19 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)

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)
$value = array('name' => 'value')
  • assert (array)
$assert = array('id' => '^[\d]+$')
  • before (array)
$before = array('before' => function() {})
  • after (array)
$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 routeName 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 routeName if a key is set
        'pattern' => '/baz',
        'controller' => 'Baz\Controller\BazController::bazAction',
        'method' => array('get', 'post', 'put', 'delete', 'options', 'head')
    )
);

$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(
        //'routeName' => 'foo', --> you can omit the routeName 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);

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 routeName, so you can omit routeName.

routes.yaml

config.routes:
    home:
        routeName: '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' => array(
                '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, there is no option set this per route at the moment before & after middleware still need to be implemented using xml and yml. (if possible)