PHPackages                             silawrenc/traffic - 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. silawrenc/traffic

ActiveLibrary[Framework](/categories/framework)

silawrenc/traffic
=================

A tiny, speedy regex powered router

0.0.1(10y ago)111ISCPHPPHP &gt;=7.0.0

Since Apr 29Pushed 10y ago1 watchersCompare

[ Source](https://github.com/silawrenc/traffic)[ Packagist](https://packagist.org/packages/silawrenc/traffic)[ RSS](/packages/silawrenc-traffic/feed)WikiDiscussions master Synced 3w ago

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

Traffic
=======

[](#traffic)

[![High five](https://camo.githubusercontent.com/54b81240307cc0b0396e2eb932f942bc9d6a890aab9b2b4097036666ca965466/687474703a2f2f692e67697068792e636f6d2f4e4c6e4976656743397635532e676966)](https://camo.githubusercontent.com/54b81240307cc0b0396e2eb932f942bc9d6a890aab9b2b4097036666ca965466/687474703a2f2f692e67697068792e636f6d2f4e4c6e4976656743397635532e676966)

[![Master branch build status](https://camo.githubusercontent.com/359136feafe308d1a78f7a05f465386a4cb83c848ae7618656da8bc82005ae39/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f73696c617772656e632f747261666669632f6d61737465722e7376673f7374796c653d666c6174)](https://travis-ci.org/silawrenc/traffic)[![PHP ~7.0](https://camo.githubusercontent.com/23656a9cbeaaad9a40c9bbd8401bb8d97f5440548f8b7263569be18118bbfec0/687474703a2f2f696d672e736869656c64732e696f2f62616467652f7068702d7e372e302d3838393242462e7376673f7374796c653d666c6174)](http://php.net)[![Published version](https://camo.githubusercontent.com/0b2748e16c8ce49cf718c66f1cd010b596c8a9237ecd5b8a2721b76c70812df3/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73696c617772656e632f747261666669632e7376673f7374796c653d666c6174)](https://packagist.org/packages/silawrenc/traffic)[![ISC Licensed](https://camo.githubusercontent.com/4dcdb982ccd648a8c33ab193fdd11467863de17f32b3bc44bf7f9d2f562ca80d/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73696c617772656e632f747261666669632e7376673f7374796c653d666c6174)](LICENSE)

**Traffic** is lightweight, but fast and flexible php web routing component. Routes are defined as regular expressions and are dispatched to a stack of callable handlers.

The easiest way to install Traffic is via [Composer](https://packagist.org/packages/silawrenc/traffic).

```
{
    "require": {
        "silawrenc/traffic": "*"
    }
}
```

API
---

[](#api)

#### Adding routes

[](#adding-routes)

Routes can be added to the router in the format `($method, $pattern, ...$handlers)` as follows.

```
$router = new Traffic;
$router->add('GET', '/foo/bar', function() {
    // do stuff
});
```

There are also convenience methods for GET and POST requests.

```
$router->get('/foo/bar', function () {
    // show stuff
});

$router->post('/foo/bar', function () {
    // save stuff
});
```

#### Dynamic routes

[](#dynamic-routes)

You can specify both the method and path as regular expressions. Captures are passed as positional arguments to handlers.

```
$router->add('(GET|POST)', '/user/([a-z]+)', function ($method, $username) {
    // as you were
});
```

A simpler syntax is also supported: you can use `/foo/{bar}` to capture part of a url (or method). The default is to match any characters except `/`. If you want to specify a pattern you can include it after a colon, e.g. `/foo/{bar:\d{4}}`. The wildcard `*` is supported for methods, and will match and capture any method name.

```
$router->add('*', '/foo/{bar}/{baz:[A-Z]+}', function ($method, $bar, $baz) {
    // important stuff
});

$router->add('(?:PATCH|PUT)', '/foo/{bar}', function ($bar) {
    // using a non-capturing regex group for the method
});
```

The best way to get a handle on supported formats is to have a look at [passing](/test/lib/matches.php) and [failing](/test/lib/misses.php) test cases. Anything that is a valid regular expression is a valid syntax for either method or path.

#### Handlers

[](#handlers)

You can specify as many handlers as you like, and they will be called in the order they are specified. If any handler returns strictly false, none of the handlers after it are invoked.

```
// return false from auth(), and render won't be invoked
$router->get('hello/world', auth(), render('landing'));

$router->get('hello/world', routeSpecificMiddleWare(), render('landing'), otherMiddleWare());
```

#### Routing

[](#routing)

Once you've added all your routes, you can use the router for matching with:

```
$router->route($method, $path);
```

This will match `$method` and `$path` against all of the routes you've added, and invoke the handlers of the first match. So be sure to specify your routes from most to least specific. The return value of the `route` method is `true` if a match is found, and `false` if not.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

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

3708d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3692872?v=4)[Simon Lawrence](/maintainers/silawrenc)[@silawrenc](https://github.com/silawrenc)

---

Top Contributors

[![silawrenc](https://avatars.githubusercontent.com/u/3692872?v=4)](https://github.com/silawrenc "silawrenc (3 commits)")

---

Tags

router

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/silawrenc-traffic/health.svg)

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

###  Alternatives

[slim/slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs

12.3k51.8M1.4k](/packages/slim-slim)[klein/klein

A lightning fast router for PHP

2.7k1.1M31](/packages/klein-klein)[slim/slim-skeleton

A Slim Framework skeleton application for rapid development

1.6k462.3k6](/packages/slim-slim-skeleton)[pecee/simple-router

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

675224.9k18](/packages/pecee-simple-router)[rareloop/router

A powerful PHP Router for PSR7 messages inspired by the Laravel API

92184.1k5](/packages/rareloop-router)[yiisoft/router

Yii router

62343.6k26](/packages/yiisoft-router)

PHPackages © 2026

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