Code Monkey home page Code Monkey logo

silex-routing-service-provider's Introduction

RoutingServiceProvider

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

silex-routing-service-provider's People

Contributors

antiseptikk avatar garretus avatar m1 avatar marcojanssen avatar richard-nl avatar samir-plusb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.