PHPackages                             binsoul/net-http-router - 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. binsoul/net-http-router

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

binsoul/net-http-router
=======================

Web router implementation for PSR-7 requests

023PHP

Since Apr 27Pushed 10y ago1 watchersCompare

[ Source](https://github.com/binsoul/net-http-router)[ Packagist](https://packagist.org/packages/binsoul/net-http-router)[ RSS](/packages/binsoul-net-http-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

net-http-router
===============

[](#net-http-router)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2590692b9adf822e948505321f446acff13e6d6b92b168abd61d02707ead8bdc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62696e736f756c2f6e65742d687474702d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/binsoul/net-http-router)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/3f01935405fd53900fb98cf3e20f198eef084b1a88cde67cd28b453dd25cbd1b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62696e736f756c2f6e65742d687474702d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/binsoul/net-http-router)

This package provides a web router implementation for PSR-7 requests. It uses matchers to extract information from the given request and returns a route object.

Your application can use the information provided by the matching route to dispatch it. A dispatcher is not provided by this package.

Install
-------

[](#install)

Via composer:

```
$ composer require binsoul/net-http-router
```

Matcher
-------

[](#matcher)

A matcher can be a closure, an object with an \_\_invoke method or an object implementing the Matcher interface.

If a matcher decides that the route was found it can optionally set an response:

```
$router->addMatcher(
    function (Route $route)
    {
        if ($route->getMissingPath() == '/hello') {
            $route->found(new Response('Hello world!'));
        }
    }
);

$route = $router->match($request); // http:/domain/hello
if ($route->hasResponse()) {
    echo $route->getResponse(); // Hello world!
}
```

### StaticMatcher

[](#staticmatcher)

The StaticMatcher matches simple paths and sets the provided route parameters of a match.

The following example:

```
$router->addMatcher(
    new StaticMatcher(
        [
            '/blog' => ['responder' => 'Blog']
        ]
    )
);

$route = $router->match($request); // http://domain/blog
var_export($route->getData());
```

would output:

```
array('responder' => 'Blog',)

```

### RegexMatcher

[](#regexmatcher)

The RegexMatcher matches arbitrary regular expressions. Named capture group are set as parameters of the route.

The following example:

```
$router->addMatcher(
    new RegexMatcher(
        [
            '/edit/(?[0-9]+)' => ['responder' => 'Edit']
        ]
    )
);

$route = $router->match($request); // http://domain/edit/1
var_export($route->getData());
```

would output:

```
array('id' => 1, 'responder' => 'Edit',)

```

### ParameterMatcher

[](#parametermatcher)

The ParameterMatcher matches paths with parameter placeholders.

Placeholders can be defined in the following format:

```
[prefix+][name][=format[(length)]][?]

```

- Prefix: Any number of characters except "\]" and "+" followed by a single "+".
- Name: A single character followed by characters, numbers or "\_".
- Format: "=" followed by a defined format name optionally followed a length definition.
- Length: A single number or a number range enclosed in parenthesis.
- Marker: If the definition ends with a single "?" the parameter is optional.

For example the path:

```
/[year=number(4)][/+month=number(1-2)?]/[name].html

```

would match:

```
/2015/09/article.html
/2015/9/article.html
/2015/article.html

```

Found placeholders are set as parameters of the route. The following example:

```
$router->addMatcher(
    new ParameterMatcher(
        [
            '/[year]/[month]/[name].html' => ['responder' => 'Blog']
        ]
    )
);

$route = $router->match($request); // http://domain/2015/09/article.html
var_export($route->getData());
```

would output:

```
array('year' => '2015', 'month' => '09', 'name' => 'article', 'responder' => 'Blog',)

```

### NamespaceMatcher

[](#namespacematcher)

The NamespaceMatcher allows to group other matchers under a common path prefix:

The following definition:

```
$matcher = new NamespaceMatcher(
    '/admin',
    [
        new StaticMatcher(
            [
                '/' => ['responder' => 'Home'],
                '/list' => ['responder' => 'List'],
            ]
        ),
        new RegexMatcher(
            [
                '/edit/(?[0-9]+)' => ['responder' => 'Edit'],
            ]
        ),
    ]
);
```

would match:

```
/admin/
/admin/list
/admin/edit/1

```

Router
------

[](#router)

Matchers can be registered either as closures or concrete objects or as strings. If a matcher is registered as a string the provided factory is used to lazily build the matcher object.

```
$router->addMatcher('AccountMatcher');
$router->setFactory($factory);

// will call $factory->buildMatcher('AccountMatcher');
$router->match($request);
```

Matchers are added to an internal queue which will be processed by the default router in the order they were added to the queue.

For example if the queue is build like:

```
$router->addMatcher('Foo'); // matches /foo
$router->addMatcher('Bar'); // matches /bar
$router->addMatcher('Baz'); // matches /baz

$router->match($request); // http://domain/foo/bar/baz
```

The route object will change like this:

```
Foo is called with missing path = '/foo/bar/baz'    and matched path = ''
Bar is called with missing path = '/bar/baz'        and matched path = '/foo'
Baz is called with missing path = '/baz'            and matched path = '/foo/bar'

```

Testing
-------

[](#testing)

```
$ composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/ea039bd8209932709ee8ab4ed6e427fde6a09b22be56aa31d823933252c97b7d?d=identicon)[binsoul](/maintainers/binsoul)

---

Top Contributors

[![binsoul](https://avatars.githubusercontent.com/u/14879936?v=4)](https://github.com/binsoul "binsoul (7 commits)")

### Embed Badge

![Health badge](/badges/binsoul-net-http-router/health.svg)

```
[![Health](https://phpackages.com/badges/binsoul-net-http-router/health.svg)](https://phpackages.com/packages/binsoul-net-http-router)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M317](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[nyholm/psr7

A fast PHP7 implementation of PSR-7

1.3k235.4M2.4k](/packages/nyholm-psr7)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M292](/packages/pusher-pusher-php-server)[spatie/crawler

Crawl all internal links found on a website

2.8k16.3M52](/packages/spatie-crawler)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78126.4M414](/packages/react-http)

PHPackages © 2026

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