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

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

httpsoft/http-router
====================

Simple and fast HTTP request router providing PSR-7 and PSR-15

1.1.1(1y ago)735.6k↑28.6%[1 issues](https://github.com/httpsoft/http-router/issues)1MITPHPPHP ^7.4|^8.0

Since Sep 27Pushed 1y ago3 watchersCompare

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

READMEChangelog (6)Dependencies (5)Versions (7)Used By (1)

HTTP Router
===========

[](#http-router)

[![License](https://camo.githubusercontent.com/66b8c25b55295d7891d07e465d977c3d8b304c14ca9b12380a4f37b8890ea040/68747470733a2f2f706f7365722e707567782e6f72672f68747470736f66742f687474702d726f757465722f6c6963656e7365)](https://packagist.org/packages/httpsoft/http-router)[![Latest Stable Version](https://camo.githubusercontent.com/661a909f00b9024ab0da12d500f8c867f7bad3de01900bbe1c103caf3729e155/68747470733a2f2f706f7365722e707567782e6f72672f68747470736f66742f687474702d726f757465722f76)](https://packagist.org/packages/httpsoft/http-router)[![Total Downloads](https://camo.githubusercontent.com/f1df81a9e163a630f070ae38cfab9ae923412e6f696a444294980bf2bccf3c1b/68747470733a2f2f706f7365722e707567782e6f72672f68747470736f66742f687474702d726f757465722f646f776e6c6f616473)](https://packagist.org/packages/httpsoft/http-router)[![GitHub Build Status](https://github.com/httpsoft/http-router/workflows/build/badge.svg)](https://github.com/httpsoft/http-router/actions)[![GitHub Static Analysis Status](https://github.com/httpsoft/http-router/workflows/static/badge.svg)](https://github.com/httpsoft/http-router/actions)[![Scrutinizer Code Coverage](https://camo.githubusercontent.com/de57a5411beae3546b346050a7908bbcc275eefe56f549e831503c7b4c2ba07d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f68747470736f66742f687474702d726f757465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/httpsoft/http-router/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/dfb1359b43e801b4bb23de7719898c28f7b9801c8900064523789986554dab81/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f68747470736f66742f687474702d726f757465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/httpsoft/http-router/?branch=master)

This package provides convenient management of HTTP request routing with support for [PSR-7](https://github.com/php-fig/http-message) and [PSR-15](https://github.com/php-fig/http-factory).

Documentation
-------------

[](#documentation)

- [In English language](https://httpsoft.org/docs/router).
- [In Russian language](https://httpsoft.org/ru/docs/router).

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

[](#installation)

This package requires PHP version 7.4 or later.

```
composer require httpsoft/http-router

```

Usage
-----

[](#usage)

```
use HttpSoft\Router\RouteCollector;

/**
 * @var mixed $handler
 */

$router = new RouteCollector();

// Defining routes.
$router->get('home', '/', $handler);
$router->post('logout', '/logout', $handler);
$router->add('login', '/login', $handler, ['GET', 'POST']);

// Custom regular expressions for placeholder parameter tokens.
$router->delete('post.delete', '/post/delete/{id}', $handler)->tokens(['id' => '\d+']);

// Generate path '/post/delete/25'
$router->routes()->path('post.delete', ['id' => 25]);
// Generate url '//example.com/post/delete/25'
$router->routes()->url('post.delete', ['id' => 25], 'example.com');
// Generate url 'https://example.com/post/delete/25'
$router->routes()->url('post.delete', ['id' => 25], 'example.com', true);
```

Set the parameter to the default value.

```
$router->get('post.view', '/post/{slug}{format}', $handler)
    ->tokens(['slug' => '[\w\-]+', 'format' => '\.[a-zA-z]{3,}'])
    ->defaults(['format' => '.html'])
;

// Generate path '/post/post-slug.html'.
$router->routes()->path('post.view', ['slug' => 'post-slug']);
```

Tokens of the route enclosed in `[...]` are considered optional.

```
$router->get('post.list', '/posts{[page]}', $handler)
    ->tokens(['page' => '\d+'])
;

// '/posts/33'
$router->routes()->path('post.list', ['page' => 33]);
// '/posts'
$router->routes()->path('post.list');
```

If necessary, you can specify a specific host for route matching.

```
// Only for example.com
$router->get('page', '/page', $handler)
    ->host('example.com')
;

// Only for subdomain.example.com
$router->get('page', '/page', $handler)
    ->host('subdomain.example.com')
;

// Only for shop.example.com or blog.example.com
$router->get('page', '/page', $handler)
    ->host('(shop|blog).example.com')
;
```

You can specify routes inside of a group.

```
$router->group('/post', static function (RouteCollector $router): void {
    // '/post/post-slug'
    $router->get('post.view', '/{slug}', ViewHandler::class)->tokens(['slug' => '[\w-]+']);
    // '/post' or '/post/2'
    $router->get('post.list', '/list{[page]}', ListHandler::class)->tokens(['page' => '\d+']);
});

// The result will be equivalent to:

$router->get('post.view', '/post/{slug}', ViewHandler::class)->tokens(['slug' => '[\w-]+']);
$router->get('post.list', '/post/list{[page]}', ListHandler::class)->tokens(['page' => '\d+']);
```

Check matching routes.

```
/**
 * @var mixed $handler
 * @var Psr\Http\Message\UriInterface $uri
 * @var Psr\Http\Message\ServerRequestInterface $request
 */

$router->get('page', '/page/{id}', $handler)->tokens(['id' => '\d+']);

// Match
$route = $router->routes()->match($request->withUri($uri->withPath('/page/11')));
$route->getMatchedParameters(); // ['id' => '11']

// Mismatch
$router->routes()->match($request->withUri($uri->withPath('/page/slug'))); // null
```

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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

Every ~311 days

Recently: every ~379 days

Total

6

Last Release

505d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7ab952aaa5ac8f82d6e96ef74c2f69082674a97eb3bc62f9d96c7304b2b4b082?d=identicon)[devanych](/maintainers/devanych)

---

Top Contributors

[![devanych](https://avatars.githubusercontent.com/u/20116244?v=4)](https://github.com/devanych "devanych (41 commits)")

---

Tags

httphttp-routerphppsr-15psr-7routerouterhttppsr-7phprouterpsr-15routehttp-router

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[league/route

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

6633.1M115](/packages/league-route)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[sunrise/http-router

A powerful solution as the foundation of your project.

16249.8k10](/packages/sunrise-http-router)[httpsoft/http-basis

Simple and fast HTTP microframework implementing PSR standards

1334.9k1](/packages/httpsoft-http-basis)[middlewares/aura-router

Middleware to use Aura.Router

1110.9k3](/packages/middlewares-aura-router)[divineniiquaye/flight-routing

Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.

152.5k](/packages/divineniiquaye-flight-routing)

PHPackages © 2026

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