PHPackages                             lloydzhou/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. [API Development](/categories/api)
4. /
5. lloydzhou/router

Abandoned → [bephp/router](/?search=bephp%2Frouter)Library[API Development](/categories/api)

lloydzhou/router
================

A fast router for PHP. It matches urls and executes PHP functions. automatic get variable based on handler function parameter list. Suport to compile router callback handlers into plain array source code.

v1.1.0(10y ago)191344MITPHPPHP &gt;=5.3.0

Since Sep 26Pushed 8y ago5 watchersCompare

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

READMEChangelogDependencies (3)Versions (25)Used By (0)

router
======

[](#router)

[![Build Status](https://camo.githubusercontent.com/443600716c0ea561cab8b36aec1b837c3f132576470bccaa9672858378b722bd/68747470733a2f2f7472617669732d63692e6f72672f62657068702f726f757465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/bephp/router)[![Coverage Status](https://camo.githubusercontent.com/3d38136c05744d8133bda3e5adf9dcc77784f492ae70e39cdbd88e252a5e9703/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f62657068702f726f757465722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/bephp/router?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/ce34ebbc4cd5ab5ee1411ef2644fefbd37dc45d5734692201c121fefc0496cba/68747470733a2f2f706f7365722e707567782e6f72672f62657068702f726f757465722f762f737461626c65)](https://packagist.org/packages/bephp/router)[![Total Downloads](https://camo.githubusercontent.com/b74b0eb45e7a0ecca90cd06eb6d3d2811da2bac075203049e9d0ced3e60f4c36/68747470733a2f2f706f7365722e707567782e6f72672f62657068702f726f757465722f646f776e6c6f616473)](https://packagist.org/packages/bephp/router)[![Latest Unstable Version](https://camo.githubusercontent.com/299b5c28ecc092b542361b5b584e015758818f69c93a9185b2f340808ba258ef/68747470733a2f2f706f7365722e707567782e6f72672f62657068702f726f757465722f762f756e737461626c65)](https://packagist.org/packages/bephp/router)[![License](https://camo.githubusercontent.com/ada318774b6f7d65e68beb2bf289c72cf8449fa4722db25c62322cc576f802f3/68747470733a2f2f706f7365722e707567782e6f72672f62657068702f726f757465722f6c6963656e7365)](https://packagist.org/packages/bephp/router)
*A barebones router for PHP.*
*It matches urls and executes PHP functions.*
*Automatic get variable based on handler function parameter list.*
*Suport to compile router callback handlers into plain array source code.*

> [中文版](https://github.com/bephp/router/blob/master/README.zh-CN.md).

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

[](#installation)

```
composer require bephp/router

```

API Reference
-------------

[](#api-reference)

### group/prefix($prefix, $hook)

[](#groupprefixprefix-hook)

add group routers with same prefix. if not pass param $prefix just reset attribute prefix of router instance to empty string. will merge the $hook to this group.

### match($method, $path, $callback, $hook)

[](#matchmethod-path-callback-hook)

create the router tree based on given $method and $path, the $callback and $hook will stored in the leaf node.

### get/post/put/delete/head/options($path, $callback, $hook)

[](#getpostputdeleteheadoptionspath-callback-hook)

wrap the match method without $method parameter. also defined "post", "put", "delete", "head" and so on.

### execute()

[](#execute)

the enter point of the application. have 3 optional parameters, $params will be merged with request variable and passed into callback handler. can pass $method and $path when not deploy as web server, can using the 2 parameters to test this library.

### error()

[](#error)

1. if call this API with $error\_code and $callback, just define the callback handler for the error code.
2. if call this API with $error\_code and other parameters, will trigger the error callback handler, the parameters will passed to the error handler.

### hook()

[](#hook)

1. if call this API with $hook\_name and $callback, just define the callback handler for the hook name.
2. if call this API with $hook\_name and other parameters, will trigger the hook handler, the parameters will passed to the hook handler.
3. there's 2 spec hook: "before" and "after", this library will auto call "before" hook before execute the handler, and call "after" hook after execute the handler.
4. the "after" hook will auto trigger with the return value of callback handler.
5. the "before" hook, and other user define hooks will auto trigger with the current router object $router, and the merged params stored in $router-&gt;params. if these hook return false, will trigger 406 error handler.

Validate
--------

[](#validate)

using [ctype functions](http://php.net/manual/zh/function.ctype-punct.php) to validate params in pathinfo
**example:**

if defined router: "/hello/:name:a.json", and using URL: "/hello/lloyd.json" to resolve url.
will call function "ctype\_alpha" to validate "lloyd".
validate command to map the ctype functions:

```
A => ctype_alnum
a => ctype_alpha
d => ctype_digit
x => ctype_xdigit
l => ctype_lower
u => ctype_upper

```

Compile
-------

[](#compile)

the PHP request always match the callback handlers every time. but the request just match one callback. so we can compile the routed tree node into plain array, to save time.

### DEV model

[](#dev-model)

using CRouter instead of Router, will always compile the source code into target file.

```
$crouter = new CRouter("router.inc.php", true);

```

### PRODUCTION model

[](#production-model)

just include the target source code, and execute it with parameters.

```
$router = include("router.inc.php");
$router->execute();

```

Performance
-----------

[](#performance)

1. using tree struct to stored callback handler on leaf node. Ensure that the time complexity of find callback function is O(log n). [![Tree Node](https://raw.githubusercontent.com/bephp/router/master/node.jpeg)](https://raw.githubusercontent.com/bephp/router/master/node.jpeg)
2. using CRouter class, suport to compile router callback handlers into plain array source code. so can save time to create tree node to store callback by split pathinfo.

### [Benchmark](https://github.com/bephp/php-router-benchmark)

[](#benchmark)

using "php-router-benchmark" to test router performance.

#### Worst-case matching

[](#worst-case-matching)

This benchmark matches the last route and unknown route. It generates a randomly prefixed and suffixed route in an attempt to thwart any optimization. 1,000 routes each with 9 arguments.

This benchmark consists of 10 tests. Each test is executed 1,000 times, the results pruned, and then averaged. Values that fall outside of 3 standard deviations of the mean are discarded.

Test NameResultsTime+ IntervalChangeRouter - unknown route (1000 routes)9930.0000232719+0.0000000000baselineRouter - last route (1000 routes)9810.0000955424+0.0000722705311% slowerFastRoute - unknown route (1000 routes)9900.0005051955+0.00048192362071% slowerFastRoute - last route (1000 routes)9980.0005567203+0.00053344842292% slowerSymfony2 Dumped - unknown route (1000 routes)9980.0006116139+0.00058834202528% slowerSymfony2 Dumped - last route (1000 routes)9980.0007765370+0.00075326513237% slowerSymfony2 - unknown route (1000 routes)9960.0028456177+0.002822345812128% slowerSymfony2 - last route (1000 routes)9930.0030129542+0.002989682312847% slowerAura v2 - last route (1000 routes)9890.1707107230+0.1706874511733450% slowerAura v2 - unknown route (1000 routes)9880.1798588730+0.1798356011772760% slowerExample
-------

[](#example)

belong is one simple example, see the full examples in [example.php](https://github.com/bephp/router/blob/master/example.php).

```
(new Router())
->error(405, function($message){
    header('Location: /hello/world', true, 302);
})
->get('/hello/:name', function($name){
    echo "Hello $name !!!";
})

```

### Start server

[](#start-server)

```
php -S 0.0.0.0:8888 example.php

```

### Test

[](#test)

Url not match, trigger 405 error handler.

```
curl -vvv 127.0.0.1:8888
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:8888
> Accept: */*
>
< HTTP/1.1 302 Found
< Host: 127.0.0.1:8888
< Connection: close
< X-Powered-By: PHP/5.5.9-1ubuntu4.12
< Location: /hello/world
< Content-type: text/html
<
* Closing connection 0

```

Url match get current result.

```
curl -vvv 127.0.0.1:8888/hello/lloyd
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> GET /hello/lloyd HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:8888
> Accept: */*
>
< HTTP/1.1 200 OK
< Host: 127.0.0.1:8888
< Connection: close
< X-Powered-By: PHP/5.5.9-1ubuntu4.12
< Content-type: text/html
<
* Closing connection 0
Hello lloyd !!!

```

Demo
----

[](#demo)

there's one [blog demo](https://github.com/bephp/blog), work with [ActiveRecord](https://github.com/bephp/activerecord) and [MicroTpl](https://github.com/bephp/microtpl).

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 96.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 ~10 days

Recently: every ~44 days

Total

21

Last Release

3686d ago

Major Versions

v0.4.4 → v1.0.02015-10-14

### Community

Maintainers

![](https://www.gravatar.com/avatar/5fe53a9780d36da291ceab6e1741680db86d0b40198cae9435c12044abfefabd?d=identicon)[lloydzhou](/maintainers/lloydzhou)

---

Top Contributors

[![lloydzhou](https://avatars.githubusercontent.com/u/1826685?v=4)](https://github.com/lloydzhou "lloydzhou (106 commits)")[![liushaohui123](https://avatars.githubusercontent.com/u/18523890?v=4)](https://github.com/liushaohui123 "liushaohui123 (2 commits)")[![wjiec](https://avatars.githubusercontent.com/u/9394086?v=4)](https://github.com/wjiec "wjiec (2 commits)")

---

Tags

microrouterlibraryfast

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[temporal/sdk

Temporal SDK

4002.2M18](/packages/temporal-sdk)[bephp/router

A fast router for PHP. It matches urls and executes PHP functions. automatic get variable based on handler function parameter list. Suport to compile router callback handlers into plain array source code.

1611.1k2](/packages/bephp-router)[vasily-kartashov/graphql-batch-processor

GraphQL batch processor

1412.9k](/packages/vasily-kartashov-graphql-batch-processor)

PHPackages © 2026

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