Download the PHP package marcojanssen/silex-routing-service-provider without Composer
On this page you can find all versions of the php package marcojanssen/silex-routing-service-provider. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download marcojanssen/silex-routing-service-provider
More information about marcojanssen/silex-routing-service-provider
Files in marcojanssen/silex-routing-service-provider
Package silex-routing-service-provider
Short Description Silex provider for adding routes
License MIT
Informations about the package silex-routing-service-provider
RoutingServiceProvider
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
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:
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
assert (array)
You can add requirements: http://silex.sensiolabs.org/doc/usage.html#requirements
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.
before (string|array)
Add a before-middleware: http://silex.sensiolabs.org/doc/middlewares.html#before-middleware
after (string|array)
Add an after-middleware: http://silex.sensiolabs.org/doc/middlewares.html#after-middleware
secure (array)
Secures a controller for the given roles: http://silex.sensiolabs.org/doc/providers/security.html#traits
Usage
Adding a single route
index.php
Adding multiple routes
index.php
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:
Using the middleware class in the route configuration
index.php
Adding a route middleware class via yml
You can add middleware classes via yml-/xml-configuration. Example:
routes.yaml
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
routes.php
index.php
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