How FastRoute Supercharges PHP Routing with One‑Pass Regex
This article explains FastRoute, a high‑performance PHP routing library created by Nikita Popov, detailing its design, core dispatcher and router mechanisms, feature set, single‑regex compilation technique, code examples, and real‑world usage within the Webman framework.
Overview
FastRoute is a lightweight, high‑performance PHP routing library that compiles all route definitions into a single regular expression, enabling a single preg_match call per request.
Architecture
The library consists of two main components:
Router – collects route definitions such as GET /post/{id:\d+} and stores the associated handler.
Dispatcher – builds a static prefix tree and hashes dynamic parts to match the incoming HTTP method and URI in constant time.
Key Features
High performance – benchmarks show routing speed significantly faster than comparable libraries.
Simplicity – a clean API allows route setup in a few lines of code.
Flexibility – supports regular‑expression constraints, custom variable rules, named routes and per‑route middleware.
No external dependencies – the library is small and easy to integrate.
Dynamic routes – e.g., /api/users/{id}.
HTTP method handling – GET, POST, PUT, DELETE for RESTful APIs.
Route Compilation
FastRoute transforms each route into a regular expression and then combines all patterns into one large expression using alternation, so only one regex evaluation is required per request.
Individual patterns
~^/users/([^/]+)$
~^/users/([^/]+)/([^/]+)$
~^/users/([^/]+)/posts/([^/]+)$Combined pattern
~^(?:
(/users/([^/]+))
| (/users/([^/]+)/([^/]+))
| (/users/([^/]+)/posts/([^/]+))
)$~xMapping between routes and captured groups can be stored in an array, for example:
[
['pattern' => '/users/{id}', 'params' => ['id']],
['pattern' => '/users/{id}/{name}', 'params' => ['id', 'name']],
['pattern' => '/users/{uid}/posts/{pid}', 'params' => ['uid', 'pid']]
]Example PHP dispatcher using the combined regex:
<?php
declare(strict_types=1);
$pattern = '~^(?:(/users/([^/]+))|(/users/([^/]+)/([^/]+))|(/users/([^/]+)/posts/([^/]+)))$~x';
$route = '/users/42/john';
if (preg_match($pattern, $route, $m)) {
// $m[1]..$m[8] contain captured groups; determine which route matched
var_dump($m);
}Real‑World Usage
Webman, a high‑performance PHP framework, uses FastRoute as its core router. The dependency is declared in composer.json:
{
"require": {
"php": ">=8.0",
"ext-json": "*",
"workerman/workerman": "^4.0.4 || ^5.0.0 || dev-master",
"nikic/fast-route": "^1.3",
"psr/container": ">=1.0"
}
}Typical route definitions in a Webman project:
Route::group('/console', function () {
Route::group('/v1', function () {
Route::group('/schedule', function () {
Route::get('/{organizationId}', [ScheduleController::class, 'list']);
Route::post('', [ScheduleController::class, 'create']);
Route::put('/{scheduleId}', [ScheduleController::class, 'edit']);
Route::delete('/{scheduleId}', [ScheduleController::class, 'delete']);
});
Route::group('/department', function () {
Route::get('/department-list/{organizationId}', [DepartmentController::class, 'departmentList']);
Route::post('', [DepartmentController::class, 'departmentCreate']);
Route::put('/{id}', [DepartmentController::class, 'departmentUpdate']);
Route::delete('', [DepartmentController::class, 'departmentDelete']);
});
});
});Further technical details are described in the author's analysis article:
https://www.npopov.com/2014/02/18/Fast-request-routing-using-regular-expressions.htmlSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
