PHPackages                             juizmill/silex-routing-service-provider - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. juizmill/silex-routing-service-provider

ActiveLibrary[Framework](/categories/framework)

juizmill/silex-routing-service-provider
=======================================

Silex provider for adding routes

1.5.0(10y ago)033MITPHP

Since Sep 25Pushed 10y ago1 watchersCompare

[ Source](https://github.com/juizmill/silex-routing-service-provider)[ Packagist](https://packagist.org/packages/juizmill/silex-routing-service-provider)[ RSS](/packages/juizmill-silex-routing-service-provider/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (10)Used By (0)

RoutingServiceProvider
======================

[](#routingserviceprovider)

[![Latest Version](https://camo.githubusercontent.com/294b91a160e6ff01099f73c1d939c039cee6eaff78ab4bc0eee3d9d88e04c155/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6d6172636f6a616e7373656e2f73696c65782d726f7574696e672d736572766963652d70726f76696465722e7376673f7374796c653d666c61742d737175617265)](https://github.com/marcojanssen/silex-routing-service-provider/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/e74466b428e8db3cd553a27bf1211c3f8eed4229e0a6b70e6c0776fdde8d9fb5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d6172636f6a616e7373656e2f73696c65782d726f7574696e672d736572766963652d70726f76696465722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/marcojanssen/silex-routing-service-provider)[![Coverage Status](https://camo.githubusercontent.com/08cf97d42e9eab07c9c9dd44766c920c63aea886b1662972d84d88922803a8fa/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d6172636f6a616e7373656e2f73696c65782d726f7574696e672d736572766963652d70726f76696465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/marcojanssen/silex-routing-service-provider/code-structure)[![Quality Score](https://camo.githubusercontent.com/f9e09a668740474683656730d72fa93d754634e2ab0dfcb34a61e2fbafca08c2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d6172636f6a616e7373656e2f73696c65782d726f7574696e672d736572766963652d70726f76696465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/marcojanssen/silex-routing-service-provider)[![Total Downloads](https://camo.githubusercontent.com/c43879ff9ed5c328ad3a320927abd50d574629ee456f97b8fa44dc7e7d1a9104/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6172636f6a616e7373656e2f73696c65782d726f7574696e672d736572766963652d70726f76696465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/marcojanssen/silex-routing-service-provider)

**RoutingServiceProvider** is a silex provider for easily adding routes

Features
--------

[](#features)

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

Installing
----------

[](#installing)

Via Composer

```
composer require marcojanssen/silex-routing-service-provider

```

Options
-------

[](#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](http://silex.sensiolabs.org/doc/usage.html#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)

You can add default values for any route variable:

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

### assert (array)

[](#assert-array)

You can add requirements:

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

### convert (array)

[](#convert-array)

You can add 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)

[](#before-stringarray)

Add a before-middleware:

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

### after (string|array)

[](#after-stringarray)

Add an after-middleware:

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

### secure (array)

[](#secure-array)

You use traits:

```
$secure = array('ROLE_ADMIN')
```

Usage
-----

[](#usage)

### Adding a single route

[](#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

[](#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

[](#adding-beforeafter-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:

[](#example-middleware-class)

```
class MiddleWare {

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

#### Using the middleware class in the route configuration

[](#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

[](#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: .

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

### Registering providers with configuration

[](#registering-providers-with-configuration)

For this example the [ConfigServiceProvider](https://github.com/igorw/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
----

[](#todo)

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

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 64.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~123 days

Recently: every ~194 days

Total

8

Last Release

3746d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/01399a69d16f1084c9111c02387bdd3ebb023d8929f1235c9be784ffdd100552?d=identicon)[juizmill](/maintainers/juizmill)

---

Top Contributors

[![marcojanssen](https://avatars.githubusercontent.com/u/2293642?v=4)](https://github.com/marcojanssen "marcojanssen (25 commits)")[![juizmill](https://avatars.githubusercontent.com/u/1959742?v=4)](https://github.com/juizmill "juizmill (5 commits)")[![samir-plusb](https://avatars.githubusercontent.com/u/4447755?v=4)](https://github.com/samir-plusb "samir-plusb (5 commits)")[![m1](https://avatars.githubusercontent.com/u/978089?v=4)](https://github.com/m1 "m1 (2 commits)")[![antiseptikk](https://avatars.githubusercontent.com/u/1520152?v=4)](https://github.com/antiseptikk "antiseptikk (1 commits)")[![Richard-NL](https://avatars.githubusercontent.com/u/116042?v=4)](https://github.com/Richard-NL "Richard-NL (1 commits)")

---

Tags

routingproviderroutesilex

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/juizmill-silex-routing-service-provider/health.svg)

```
[![Health](https://phpackages.com/badges/juizmill-silex-routing-service-provider/health.svg)](https://phpackages.com/packages/juizmill-silex-routing-service-provider)
```

###  Alternatives

[pecee/simple-router

Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.

696214.6k17](/packages/pecee-simple-router)[izniburak/router

simple router class for php

23522.6k7](/packages/izniburak-router)[ddesrosiers/silex-annotation-provider

A silex service provider that allows the use of annotations in ServiceControllers.

25246.7k3](/packages/ddesrosiers-silex-annotation-provider)[ecoal95/php-router

Minimal routing library

271.0k1](/packages/ecoal95-php-router)[developermarius/simple-router

Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.

112.4k](/packages/developermarius-simple-router)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
