PHPackages                             slince/routing - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. slince/routing

ActiveLibrary[HTTP &amp; Networking](/categories/http)

slince/routing
==============

A flexible web routing component

1.0.0(8y ago)5471MITPHPPHP &gt;=5.6.0

Since May 25Pushed 8y ago1 watchersCompare

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

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

Routing Component
=================

[](#routing-component)

[![Build Status](https://camo.githubusercontent.com/ef2b64ce95098582a7c393f1d1c2dd2c84e09a8256fd58e1aec22a3654ff51a3/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f736c696e63652f726f7574696e672f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/slince/routing)[![Coverage Status](https://camo.githubusercontent.com/36c8afb6de817d29c8455d5b16e3c597f0146c18f4f5f6afa65331f67bba1eef/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f736c696e63652f726f7574696e672e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/slince/routing)[![Latest Stable Version](https://camo.githubusercontent.com/3ba7f65a7b732f7b18b540d49ae02d41c9ec1c91813c9facbd4550478363ff55/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736c696e63652f726f7574696e672e7376673f7374796c653d666c61742d737175617265266c6162656c3d737461626c65)](https://packagist.org/packages/slince/routing)[![Scrutinizer](https://camo.githubusercontent.com/1eae6bfe4f411b9fb92623ff36ef95042ddeced9a457006e5513919972ccc785/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f736c696e63652f726f7574696e672e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/slince/routing/?branch=master)

A flexible web routing component.

Installation
------------

[](#installation)

Install via composer

```
composer require slince/routing
```

Quick example
-------------

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

Usage
-----

[](#usage)

### Defines routes

[](#defines-routes)

#### Creates an instance of `Slince\Routing\RouteCollection` first,

[](#creates-an-instance-of-slinceroutingroutecollection-first)

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

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

```
$route->setRequirements([
    'id' => '\d+'
]);
```

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

```
$route->setDefaults([
    'id' => 1
]);
```

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

#### Shorthands for HTTP methods are also provided.

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

[](#customize-http-verb)

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

### Host matching

[](#host-matching)

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

```
$routes->create('/products', 'Products::index')
    ->setHost('product.domain.com');
```

The route will only match the request with `product.domain.com` domain

### Force route use HTTPS or HTTP

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

[](#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) {
    //404
}
```

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) {
    //403
    var_dump($e->getAllowedMethods());
} catch (Slince\Routing\Exception\RouteNotFoundException $e) {
    //404
}
```

### Generate path for a route

[](#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();
$generator->setRequest($request);

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

License
-------

[](#license)

The MIT license. See [MIT](https://opensource.org/licenses/MIT)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

3276d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3785826?v=4)[Tao](/maintainers/slince)[@slince](https://github.com/slince)

---

Top Contributors

[![slince](https://avatars.githubusercontent.com/u/3785826?v=4)](https://github.com/slince "slince (37 commits)")

---

Tags

matchingpsr-7routerouterroutingpsr-7routerroutingroute

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/slince-routing/health.svg)

```
[![Health](https://phpackages.com/badges/slince-routing/health.svg)](https://phpackages.com/packages/slince-routing)
```

###  Alternatives

[aura/router

Powerful, flexible web routing for PSR-7 requests.

5231.5M67](/packages/aura-router)[league/route

Fast routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

6633.1M115](/packages/league-route)[pmjones/auto-route

Automatically routes HTTP request to action classes.

20158.6k6](/packages/pmjones-auto-route)[miladrahimi/phprouter

A powerful, lightweight, and very fast HTTP URL router for PHP projects.

20832.6k2](/packages/miladrahimi-phprouter)[contributte/api-router

RESTful Router for your Apis in Nette Framework - created either directly or via attributes

20802.8k3](/packages/contributte-api-router)

PHPackages © 2026

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