PHPackages                             divineniiquaye/flight-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. divineniiquaye/flight-routing

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

divineniiquaye/flight-routing
=============================

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

v2.1.0(3y ago)152.5k4[3 issues](https://github.com/divineniiquaye/flight-routing/issues)[2 PRs](https://github.com/divineniiquaye/flight-routing/pulls)BSD-3-ClausePHPPHP ^8.0

Since Apr 27Pushed 3y ago4 watchersCompare

[ Source](https://github.com/divineniiquaye/flight-routing)[ Packagist](https://packagist.org/packages/divineniiquaye/flight-routing)[ Docs](https://www.divinenii.com)[ Fund](https://biurad.com/sponsor)[ Patreon](https://www.patreon.com/biurad)[ RSS](/packages/divineniiquaye-flight-routing/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (10)Dependencies (14)Versions (35)Used By (0)

The PHP HTTP Flight Routing
===========================

[](#the-php-http-flight-routing)

[![PHP Version](https://camo.githubusercontent.com/5f47747cca6b1c002c809cf3ac813d5afd2a763897c19f8775b41ed6b1467564/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f646976696e656e696971756179652f666c696768742d726f7574696e672e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d253233383839324246)](http://php.net)[![Latest Version](https://camo.githubusercontent.com/1b45026a219b450d61aed3c7b562f773aa87cacb9b0148bf841d3ea78244e2ca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646976696e656e696971756179652f666c696768742d726f7574696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/divineniiquaye/flight-routing)[![Workflow Status](https://camo.githubusercontent.com/ed6c11b85e7038761c9885389bc8eb8496937c035731ea49b0c18f777b2fbc9b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f646976696e656e696971756179652f666c696768742d726f7574696e672f6275696c643f7374796c653d666c61742d737175617265)](https://github.com/divineniiquaye/flight-routing/actions?query=workflow%3Abuild)[![Code Maintainability](https://camo.githubusercontent.com/3777011c998e9a8a6da4ee6c40f594dc7d884345e6a80740b035265c168803bc/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6d61696e7461696e6162696c6974792f646976696e656e696971756179652f666c696768742d726f7574696e673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/divineniiquaye/flight-routing)[![Coverage Status](https://camo.githubusercontent.com/6e2b6f604bece242af7ae5b96274ae49e45b863c9c34bb8dbae0855fefc36478/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f646976696e656e696971756179652f666c696768742d726f7574696e673f7374796c653d666c61742d737175617265)](https://codecov.io/gh/divineniiquaye/flight-routing)[![Psalm Type Coverage](https://camo.githubusercontent.com/7facd2e46e1fe9947c8ecbe69e238e3bea532861bad3a35a45612cfc78e9e818/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742d7371756172652675726c3d687474707325334125324625324673686570686572642e646576253246676974687562253246646976696e656e69697175617965253246726164652d6469253246636f766572616765)](https://shepherd.dev/github/divineniiquaye/flight-routing)[![Quality Score](https://camo.githubusercontent.com/885954599071135c4c98a2f0b092506f110340a8c3a3794d7434e8e4fd16266f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f646976696e656e696971756179652f666c696768742d726f7574696e672e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/divineniiquaye/flight-routing)

---

Flight routing is yet another high performance HTTP router for [PHP](https://php.net). It is simple, easy to use, scalable and fast. This library depends on [PSR-7](http://www.php-fig.org/psr/psr-7/) for route match and support using [PSR-15](http://www.php-fig.org/psr/psr-15/) for intercepting route before being rendered.

This library previous versions was inspired by [Sunrise Http Router](https://github.com/sunrise-php/http-router), [Symfony Routing](https://github.com/symfony/routing), [FastRoute](https://github.com/nikic/FastRoute) and now completely rewritten for better performance.

🏆 Features
----------

[](#-features)

- Supports all HTTP request methods (eg. `GET`, `POST` `DELETE`, etc).
- Regex Expression constraints for parameters.
- Reversing named routes paths to full URL with strict parameter checking.
- Route grouping and merging.
- Supports routes caching for performance.
- [PSR-15](http://www.php-fig.org/psr/psr-15/) Middleware (classes that intercepts before the route is rendered).
- Domain and sub-domain routing.
- Restful Routing.
- Supports PHP 8 attribute `#[Route]` and doctrine annotation `@Route` routing.
- Support custom matching strategy using custom route matcher class or compiler class.

📦 Installation
--------------

[](#-installation)

This project requires [PHP](https://php.net) 8.0 or higher. The recommended way to install, is via [Composer](https://getcomposer.org). Simply run:

```
$ composer require divineniiquaye/flight-routing
```

I recommend reading [my blog post](https://divinenii.com/blog/php-web_server_configuration) on setting up Apache, Nginx, IIS server configuration for your [PHP](https://php.net) project.

📍 Quick Start
-------------

[](#-quick-start)

The default compiler accepts the following constraints in route pattern:

- `{name}` - required placeholder.
- `{name=foo}` - placeholder with default value.
- `{name:regex}` - placeholder with regex definition.
- `{name:regex=foo}` - placeholder with regex definition and default value.
- `[{name}]` - optional placeholder.

A name of a placeholder variable is simply an acceptable PHP function/method parameter name expected to be unique, while the regex definition and default value can be any string (i.e \[^/\]+).

- `/foo/` - Matches **/foo/** or **/foo**. ending trailing slashes are striped before matching.
- `/user/{id}` - Matches **/user/bob**, **/user/1234** or **/user/23/**.
- `/user/{id:[^/]+}` - Same as the previous example.
- `/user[/{id}]` - Same as the previous example, but also match **/user** or **/user/**.
- `/user[/{id}]/` - Same as the previous example, ending trailing slashes are striped before matching.
- `/user/{id:[0-9a-fA-F]{1,8}}` - Only matches if the id parameter consists of 1 to 8 hex digits.
- `/files/{path:.*}` - Matches any URL starting with **/files/** and captures the rest of the path into the parameter **path**.
- `/[{lang:[a-z]{2}}[-{sublang}]/]{name}[/page-{page=0}]` - Matches **/cs/hello**, **/en-us/hello**, **/hello**, **/hello/page-12**, or **/ru/hello/page-23**

Route pattern accepts beginning with a `//domain.com` or `https://domain.com`. Route path also support adding controller (i.e `*`) directly at the end of the route path:

- `*` - translates as a callable of BlogController class with method named indexAction.
- `*` - translates as a function, if a handler class is defined in route, then it turns to a callable.

Here is an example of how to use the library:

```
use Flight\Routing\{Router, RouteCollection};

$router = new Router();
$router->setCollection(static function (RouteCollection $routes) {
    $routes->add('/blog/[{slug}]', handler: [BlogController::class, 'indexAction'])->bind('blog_show');
    //... You can add more routes here.
});
```

Incase you'll prefer declaring your routes outside a closure scope, try this example:

```
use Flight\Routing\{Router, RouteCollection};

$routes = new RouteCollection();
$routes->get('/blog/{slug}*', handler: BlogController::class)->bind('blog_show');

$router = Router::withCollection($routes);
```

> NB: If caching is enabled, using the router's `setCollection()` method has much higher performance than using the `withCollection()` method.

By default Flight routing does not ship a [PSR-7](http://www.php-fig.org/psr/psr-7/) http library nor a library to send response headers and body to the browser. If you'll like to install this libraries, I recommend installing either [biurad/http-galaxy](https://github.com/biurad/php-http-galaxy) or [nyholm/psr7](https://github.com/nyholm/psr7) and [laminas/laminas-httphandlerrunner](https://github.com/laminas/laminas-httphandlerrunner).

```
$request = ... // A PSR-7 server request initialized from global request

// Routing can match routes with incoming request
$route = $router->matchRequest($request);
// Should return an array, if request is made on a a configured route path (i.e /blog/lorem-ipsum)

// Routing can also generate URLs for a given route
$url = $router->generateUri('blog_show', ['slug' => 'my-blog-post']);
// $url = '/blog/my-blog-post' if stringified else return a GeneratedUri class object
```

In this example below, I'll assume you've installed [nyholm/psr-7](https://github.com/nyholm/psr7) and [laminas/laminas-httphandlerrunner](https://github.com/laminas/laminas-httphandlerrunner), So we can use [PSR-15](http://www.php-fig.org/psr/psr-15/) to intercept route before matching and [PSR-17](https://www.php-fig.org/psr/psr-17/) to render route response onto the browser:

```
use Flight\Routing\Handlers\RouteHandler;
use Laminas\HttpHandlerRunner\Emitter\SapiStreamEmitter;

$router->pipe(...); # Add PSR-15 middlewares ...

$handlerResolver = ... // You can add your own route handler resolver else default is null
$responseFactory = ... // Add PSR-17 response factory
$request = ... // A PSR-7 server request initialized from global request

// Default route handler, a custom request route handler can be used also.
$handler = new RouteHandler($responseFactory, $handlerResolver);

// Match routes with incoming request and return a response
$response = $router->process($request, $handler);

// Send response to the browser ...
(new SapiStreamEmitter())->emit($response);
```

To use [PHP](https://php.net) 8 attribute support, I highly recommend installing [biurad/annotations](https://github.com/biurad/php-annotations) and if for some reason you decide to use [doctrine/annotations](https://github.com/doctrine/annotations) I recommend you install [spiral/attributes](https://github.com/spiral/attributes) to use either one or both.

An example using annotations/attribute is:

```
use Biurad\Annotations\AnnotationLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Flight\Routing\Annotation\Listener;
use Spiral\Attributes\{AnnotationReader, AttributeReader};
use Spiral\Attributes\Composite\MergeReader;

$reader = new AttributeReader();

// If you only want to use PHP 8 attribute support, you can skip this step and set reader to null.
if (\class_exists(AnnotationRegistry::class)) $reader = new MergeReader([new AnnotationReader(), $reader]);

$loader = new AnnotationLoader($reader);
$loader->listener(new Listener(), 'my_routes');
$loader->resource('src/Controller', 'src/Bundle/BundleName/Controller');

$annotation = $loader->load('my_routes'); // Returns a Flight\Routing\RouteCollection class instance
```

You can add more listeners to the annotation loader class to have all your annotations/attributes loaded from one place. Also use either the `populate()` route collection method or `group()` to merge annotation's route collection into default route collection, or just simple use the annotation's route collection as your default router route collection.

Finally, use a restful route, refer to this example below, using `Flight\Routing\RouteCollection::resource`, method means, route becomes available for all standard request methods `Flight\Routing\Router::HTTP_METHODS_STANDARD`:

```
namespace Demo\Controller;

class UserController {
    public function getUser(int $id): string {
        return "get {$id}";
    }

    public function postUser(int $id): string {
        return "post {$id}";
    }

    public function deleteUser(int $id): string {
        return "delete {$id}";
    }
}
```

Add route using `Flight\Routing\Handlers\ResourceHandler`:

```
use Flight\Routing\Handlers\ResourceHandler;

$routes->add('/user/{id:\d+}', ['GET', 'POST'], new ResourceHandler(Demo\UserController::class, 'user'));
```

As of Version 2.0, flight routing is very much stable and can be used in production, Feel free to contribute to the project, report bugs, request features and so on.

> Kindly take note of these before using:
>
> - Avoid declaring the same pattern of dynamic route multiple times (eg. `/hello/{name}`), instead use static paths if you choose use same route path with multiple configurations.
> - Route handlers prefixed with a `\` (eg. `\HelloClass` or `['\HelloClass', 'handle']`) should be avoided if you choose to use a different resolver other the default handler's RouteInvoker class.
> - If you decide again to use a custom route's handler resolver, I recommend you include the static `resolveRoute` method from the default's route's RouteInvoker class.

📓 Documentation
---------------

[](#-documentation)

In-depth documentation on how to use this library, kindly check out the [documentation](https://divinenii.com/courses/php-flight-routing/) for this library. It is also recommended to browse through unit tests in the [tests](./tests/) directory.

🙌 Sponsors
----------

[](#-sponsors)

If this library made it into your project, or you interested in supporting us, please consider [donating](https://divinenii.com/sponser) to support future development.

👥 Credits &amp; Acknowledgements
--------------------------------

[](#-credits--acknowledgements)

- [Divine Niiquaye Ibok](https://github.com/divineniiquaye) is the author this library.
- [All Contributors](https://github.com/divineniiquaye/flight-routing/contributors) who contributed to this project.

📄 License
---------

[](#-license)

Flight Routing is completely free and released under the [BSD 3 License](LICENSE).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 99.4% 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 ~31 days

Recently: every ~14 days

Total

31

Last Release

1254d ago

Major Versions

0.5.x-dev → v1.0.02020-12-21

v1.6.4 → v2.0.02022-10-10

PHP version history (6 changes)v0.1-betaPHP ^7.1.3

v0.2.0PHP ^7.1

v0.2.9PHP ^7.2

v0.5.0PHP ^7.1 || ^8.0

v1.4.0PHP ^7.4 || ^8.0

v2.0.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![divineniiquaye](https://avatars.githubusercontent.com/u/53147395?v=4)](https://github.com/divineniiquaye "divineniiquaye (792 commits)")[![renovate-bot](https://avatars.githubusercontent.com/u/25180681?v=4)](https://github.com/renovate-bot "renovate-bot (2 commits)")[![cyclonecopp](https://avatars.githubusercontent.com/u/46801032?v=4)](https://github.com/cyclonecopp "cyclonecopp (1 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

biuradhttp-routermiddlewarephppsr-15psr-7routerroutingpsr-7phprouterpsr-15biuradurl-routingphp-17

###  Code Quality

TestsPest

Static AnalysisPHPStan, Psalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[league/route

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

6633.1M115](/packages/league-route)[sunrise/http-router

A powerful solution as the foundation of your project.

16249.8k10](/packages/sunrise-http-router)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[httpsoft/http-basis

Simple and fast HTTP microframework implementing PSR standards

1334.9k1](/packages/httpsoft-http-basis)

PHPackages © 2026

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