Code Monkey home page Code Monkey logo

silex-routing-service-provider's Introduction

RoutingServiceProvider


Build Status Scrutinizer Quality Score Code Coverage

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

  • Install Composer

  • Add marcojanssen/silex-routing-service-provider to your composer.json:

{
    "require": {
        "marcojanssen/silex-routing-service-provider": "1.2.*"
    }
}
  • Install/update your dependencies

Options

Each route is required to have the following parameters:

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

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(
    '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(
    array(
        'pattern' => '/foo',
        'controller' => 'Foo\Controller\FooController::fooAction',
        'method' => array('get', 'post', 'put', 'delete', 'options', 'head')
    ),
    array(
        '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(
    array(
        '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

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 RoutingServiceProvider(__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.

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)

silex-routing-service-provider's People

Contributors

marcojanssen avatar richard-nl avatar

Watchers

Javier Lopez Lopez avatar James Cloos 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.