RoutingServiceProvider
, (*1)
RoutingServiceProvider is a silex provider for easily adding routes, (*2)
Features
- Register providers through configuration
- Register multiple providers with the provider
- Register a single provider with the provider
Installing
Via Composer, (*3)
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), (*4)
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:, (*5)
$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:, (*6)
value (array)
You can add default values for any route variable: http://silex.sensiolabs.org/doc/usage.html#default-values, (*7)
``` php
$value = array('name' => 'value'), (*8)
### assert (array)
You can add requirements: http://silex.sensiolabs.org/doc/usage.html#requirements
``` php
$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., (*9)
``` php
$after = array('convert' => function() {}), (*10)
### before (string|array)
Add a before-middleware: http://silex.sensiolabs.org/doc/middlewares.html#before-middleware
``` php
$before = array('before' => function() {})
after (string|array)
Add an after-middleware: http://silex.sensiolabs.org/doc/middlewares.html#after-middleware, (*11)
``` php
$after = array('after' => function() {}), (*12)
### secure (array)
Secures a controller for the given roles: http://silex.sensiolabs.org/doc/providers/security.html#traits
``` php
$secure = array('ROLE_ADMIN')
Usage
Adding a single route
index.php
, (*13)
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
, (*14)
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., (*15)
Note Be aware that currently there is only support for php., (*16)
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
, (*17)
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:, (*18)
routes.yaml
, (*19)
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., (*20)
ATTENTION: Unfortunately with this way, you cannot use the Ā“__invokeĀ“ method described above., (*21)
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., (*22)
routes.yaml
, (*23)
config.routes:
home:
name: 'home'
pattern: /
method: [ 'get', 'post' ]
controller: 'Foo\Controllers\FooController::getAction'
routes.php
, (*24)
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
, (*25)
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., (*26)
Todo
- convert: route variable converters need to be services. We need a way to add them as a valid callback like
MyNamespace\MyClass::converter