PHPackages                             fastd/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. [Framework](/categories/framework)
4. /
5. fastd/routing

ActiveLibrary[Framework](/categories/framework)

fastd/routing
=============

FastD routing component

v3.2.6(4y ago)1419.7k7[1 PRs](https://github.com/fastdlabs/routing/pulls)2MITPHPPHP &gt;=5.6CI failing

Since Jun 20Pushed 3mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (3)Versions (37)Used By (2)

FastD Routing
=============

[](#fastd-routing)

[![Build Status](https://camo.githubusercontent.com/1a1e63d68cf39c353b924af4da9b88c984f6b84b5789e314bfbc9029f0a9151e/68747470733a2f2f7472617669732d63692e6f72672f66617374646c6162732f726f7574696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/fastdlabs/routing)[![Latest Stable Version](https://camo.githubusercontent.com/3620fa225e8768c072c1f25ec90950f68031a893e5449f3ef4fe2a3f3dc7073e/68747470733a2f2f706f7365722e707567782e6f72672f66617374642f726f7574696e672f762f737461626c65)](https://packagist.org/packages/fastd/routing)[![Total Downloads](https://camo.githubusercontent.com/b07892eca0674cbff41c61fe86451c40c4344f121502af716690aef58fa79055/68747470733a2f2f706f7365722e707567782e6f72672f66617374642f726f7574696e672f646f776e6c6f616473)](https://packagist.org/packages/fastd/routing)[![Latest Unstable Version](https://camo.githubusercontent.com/43b23e26f82fa5e4206d051d982aa9cd5320792313699ee6cea669f3d9fee853/68747470733a2f2f706f7365722e707567782e6f72672f66617374642f726f7574696e672f762f756e737461626c65)](https://packagist.org/packages/fastd/routing)[![License](https://camo.githubusercontent.com/4342484bc730017f779b4242099023bb8a7b28e4a440fdced09ce7c0c1924e4e/68747470733a2f2f706f7365722e707567782e6f72672f66617374642f726f7574696e672f6c6963656e7365)](https://packagist.org/packages/fastd/routing)

Simple PHP router that supports routing nesting, dynamic routing, fuzzy routing, middleware, and more. Relies on the [http](https://github.com/JanHuang/http) component.

Claim
-----

[](#claim)

- PHP 7.2

Composer
--------

[](#composer)

```
Composer require "fastd/routing"

```

Use
---

[](#use)

You can set the route through the `RouteCollection` object, or you can create a route through the route list. Detailed documentation: [fastd/routing](docs/zh_CN/readme.md)

### Static routing

[](#static-routing)

```
use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/', function () {
    return 'hello world';
});

$route = $collection->match(new ServerRequest('GET', '/')); // \FastD\Routing\Route

echo call_user_func_array($route->getCallback(), []);
```

Route matching does not call the callback of the route, but returns the entire Route for callback processing, so `match` only returns the matching route object.

### Dynamic routing

[](#dynamic-routing)

```
use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/{name}', function ($name) {
    return 'hello ' . $name;
});

$route = $collection->match(new ServerRequest('GET', '/foo')); // \FastD\Routing\Route

echo call_user_func_array($route->getCallback(), $route->getParameters());
```

Under dynamic routing, the successfully matched route will update the matching parameters to `getParameters`, and get the matching parameter information through `getParameters`.

### Same route, multiple methods

[](#same-route-multiple-methods)

```
$collection = new FastD\Routing\RouteCollection();
$collection->get('/', function () {
    return 'hello GET';
});
$collection->post('/', function () {
    return 'hello POST';
});
$response = $collection->dispatch('GET', '/'); // hello GET
$response = $collection->dispatch('POST', '/'); // hello POST
```

### Hybrid routing

[](#hybrid-routing)

In many cases, our route may only be one parameter difference. Here is an example.

```
use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/{name}', function () {
    return 'get1';
});

$collection->addRoute('GET', '/', function () {
    return 'get2';
});

$route = $collection->match(new ServerRequest('GET', '/abc')); // \FastD\Routing\Route
$route2 = $collection->match(new ServerRequest('GET', '/')); // \FastD\Routing\Route
echo call_user_func_array($route->getCallback(), $route->getParameters());
echo call_user_func_array($route2->getCallback(), $route2->getParameters());
```

### Routing Group

[](#routing-group)

The routing group will add its own routing prefix to each of your sub-routing dollars, supporting multiple levels of nesting.

```
use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->group('/v1', function (RouteCollection $collection) {
    $collection->addRoute('GET', '/{name}', function () {
        return 'get1';
    });
});

$route = $collection->match(new ServerRequest('GET', '/v1/abc'));

echo call_user_func_array($route->getCallback(), $route->getParameters());
```

### Fuzzy routing

[](#fuzzy-routing)

The inspiration for fuzzy routing comes from the onRequest callback of the Swoole http server. Because each route entry passes onRequest, when it is created, there may be some special routes processed according to pathinfo. Then the fuzzy route can be sent. It’s time to use.

```
use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/api/*', function ($path) {
    return $path;
});

$route = $collection->match(new ServerRequest('GET', '/api/abc'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // /abc

$route = $collection->match(new ServerRequest('GET', '/api/cba'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // /cba
```

Match all legal routes that start with `/api` and then callback

### Routing middleware

[](#routing-middleware)

The routing component implements routing middleware based on \[Http\] () and \[HTTP Middlewares\] ().

> Routing middleware callbacks automatically call back `Psr\Http\Message\ServerRequestInterface` and `FastD\Middleware\DelegateInterface` as arguments.

After the middleware call is completed, the `\Psr\Http\Message\ResponseInterface` object is returned for the program to process the output.

```
use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/api/*', function (ServerRequest $request) {
    return 'hello';
});

$dispatcher = new \FastD\Routing\RouteDispatcher($collection);

$response = $dispatcher->dispatch(new ServerRequest('GET', '/api/abc'));

echo $response->getBody();
```

Testing
-------

[](#testing)

```
phpunit

```

### Contribution

[](#contribution)

I am very pleased to be interested and willing to participate in the creation of a better PHP ecosystem, the developer of the Swoole Eco.

If you are happy with this, but don't know how to get started, you can try the following things:

- Problems encountered in your system \[Feedback\] ().
- Have better suggestions? Feel free to contact \[\] () or \[Sina Weibo: Coding Man\] ().

### Contact

[](#contact)

If you encounter problems during use, please contact: . Weibo: \[Coding Man\] ()

License MIT
-----------

[](#license-mit)

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance54

Moderate activity, may be stable

Popularity33

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 90.8% 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 ~72 days

Recently: every ~202 days

Total

35

Last Release

1517d ago

Major Versions

v0.1.2 → v2.0.0-beta12016-05-01

v2.0.3 → v3.0.0-rc12017-01-10

v3.2.2 → 5.0.x-dev2020-01-06

PHP version history (4 changes)v0.1.0PHP &gt;=5.4

v2.0.0-beta1PHP &gt;=7.0

v3.0.0-rc1PHP &gt;=5.6

5.0.x-devPHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/94c2bc821caf23977e1c3deea85e3cbc9a73a632e1afaf778638f7fe9da1c42b?d=identicon)[JanHuang](/maintainers/JanHuang)

---

Top Contributors

[![JanHuang](https://avatars.githubusercontent.com/u/7090871?v=4)](https://github.com/JanHuang "JanHuang (266 commits)")[![Gu1K](https://avatars.githubusercontent.com/u/6781018?v=4)](https://github.com/Gu1K "Gu1K (9 commits)")[![gmapnl](https://avatars.githubusercontent.com/u/41949435?v=4)](https://github.com/gmapnl "gmapnl (7 commits)")[![RunnerLee](https://avatars.githubusercontent.com/u/7436388?v=4)](https://github.com/RunnerLee "RunnerLee (5 commits)")[![longxinH](https://avatars.githubusercontent.com/u/12230340?v=4)](https://github.com/longxinH "longxinH (4 commits)")[![loyating](https://avatars.githubusercontent.com/u/5088390?v=4)](https://github.com/loyating "loyating (1 commits)")[![wangsir0624](https://avatars.githubusercontent.com/u/19991740?v=4)](https://github.com/wangsir0624 "wangsir0624 (1 commits)")

---

Tags

phprestfulroutingrouting

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[klein/klein

A lightning fast router for PHP

2.7k1.1M31](/packages/klein-klein)[nette/application

🏆 Nette Application: a full-stack component-based MVC kernel for PHP that helps you write powerful and modern web applications. Write less, have cleaner code and your work will bring you joy.

44615.4M983](/packages/nette-application)[laravel/folio

Page based routing for Laravel.

608453.9k27](/packages/laravel-folio)[pecee/simple-router

Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.

696214.6k17](/packages/pecee-simple-router)[vlucas/bulletphp

A heierarchical resource-oriented micro-framework built on nested closures instead of route-based callbacks

41949.9k1](/packages/vlucas-bulletphp)[luthier/luthier

Improved routing, middleware support, authentication tools and more for CodeIgniter 3 framework

150108.8k](/packages/luthier-luthier)

PHPackages © 2026

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