A flexible web routing component

1.0.0 2017-05-25 04:03 UTC

This package is auto-updated.

Last update: 2024-02-08 16:20:03 UTC


Build Status Coverage Status Latest Stable Version Scrutinizer

A flexible web routing component.


Install via composer

composer require slince/routing

Quick example

$routes = new Slince\Routing\RouteCollection();
$routes->get('/products', 'Products::index')->setName('product_index');

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(); //Creates the psr7 request instance

$matcher = new Slince\Routing\Matcher($routes);
$generator = new Slince\Routing\Generator($request);

$route = $matcher->matchRequest($request); //Matches the current request
var_dump($route->getComputedParamters()); //Dumps route computed paramters
echo $generator->generate($route); //Generates path 

$route = $routes->getByAction('Products::index');
echo $generator->generate($route); //Generates path 

$route = $routes->getByName('product_index');
echo $generator->generate($route); //Generates path 


Defines routes

Creates an instance of Slince\Routing\RouteCollection first,

$routes = new Slince\Routing\RouteCollection();
$route = new Slince\Routing\Route('/products/{id}', 'Products::view');

The route path contain the placeholder {id} which matches everything except "/" and "." You can set custom requirements with setRequirement method or setRequirements method.

    'id' => '\d+'

Routing supports optional placeholder, you can provide a default value for the placeholder.

    'id' => 1

The route can match /products and /products/1.

Shorthands for HTTP methods are also provided.

$routes = new RouteCollection();

$routes->get('/pattern', 'action');
$routes->post('/pattern', 'action');
$routes->put('/pattern', 'action');
$routes->delete('/pattern', 'action');
$routes->options('/pattern', 'action');
$routes->patch('/pattern', 'action');

Customize HTTP verb

$route->setMethods(['GET', 'POST', 'PUT']);

Host matching

You can limit a route to specified host with setHost method.

$routes->create('/products', 'Products::index')

The route will only match the request with domain

Force route use HTTPS or HTTP

Routing also allow you to define routes using http and https.

$routes = new Slince\Routing\RouteCollection();

$routes->https('/pattern', 'action');
$routes->http('/pattern', 'action');

Or customize this.

$route->setSchemes(['http', 'https']);

Match a path or psr7 request.

$routes = new Slince\Routing\RouteCollection();
$routes->create('/products/{id}.{_format}', 'Products::view');
$matcher = new Slince\Routing\Matcher($routes);

try {
    $route = $matcher->match('/products/10.html');
    print_r($route->getComputedParameters())// ['id' => 10, '_format' => 'html']
} catch (Slince\Routing\Exception\RouteNotFoundException $e) {

Matcher will return the matching route. If no matching route can be found, matcher will throw a RouteNotFoundException.

Match a Psr\Http\Message\ServerRequestInterface.

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
try {
    $route = $matcher->matchRequest($request);
} catch (Slince\Routing\Exception\MethodNotAllowedException $e) {
} catch (Slince\Routing\Exception\RouteNotFoundException $e) {

Generate path for a route

$generator = new Slince\Routing\Generator();

$route = new Slince\Routing\Route('/foo/{id}', 'action');
echo $generator->generate($route, ['id' => 10]); //will output "/foo/10"

If you want generate the absolute url for the route, you need to provide generator with a request as request context.

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();

echo $generator->generate($route, ['id' => 10], true); //will output "{scheme}://{domain}/foo/10"


The MIT license. See MIT