pipe / router
Blazingly fast php router, route parser
v0.2.1
2026-12-02 15:50 UTC
Requires
- php: >=8.2
Requires (Dev)
- pestphp/pest: ^3.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Simple, pure PHP URL pattern router
Installation
composer require pipe/router
Requires PHP 8.2 or newer. No runtime dependencies.
Usage
use Pipe\Router; $router = (new Router()) ->match('GET', '/', 'home.php') ->match(['GET', 'POST'], '/users/{id}', 'user.php') ->any('/articles/{slug},{id}.html', 'article.php') ->any('/docs/{...path}', 'docs.php'); $result = $router->resolve($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); if ($result->resolution === Router::FOUND) { $handler = $result->handler; // whatever you registered: a string, a callable, ... $args = $result->args; // ['id' => '42'] }
The router only matches strings. It never touches the filesystem and never calls handlers.
API
| Method | Description |
|---|---|
match(string|array $methods, string $pattern, mixed $handler): self |
Register a handler for one or more methods. Methods are case-insensitive. Registering the same method and pattern again replaces the handler. Throws InvalidArgumentException for malformed patterns. |
any(string $pattern, mixed $handler): self |
Register a handler for every method (Router::ANY). |
resolve(string $method, string $uri): object |
Returns {resolution, handler, args, pattern}. resolution is Router::FOUND or Router::NOT_FOUND. On a miss, handler and pattern are null and args is []. |
routes(): array |
The registered routes as method => [pattern => handler]. |
Router::getInstance() / Pipe\route() |
A shared instance, for applications that want one. |
Pattern syntax
| Pattern | Matches | args |
|---|---|---|
/about |
/about, /about/ |
[] |
/users/{id} |
/users/42 (not /users/42/x) |
['id' => '42'] |
/articles/{slug},{id}.html |
/articles/hello,7.html |
['slug' => 'hello', 'id' => '7'] |
/articles/article-{id} |
/articles/article-9 |
['id' => '9'] |
/feed.xml |
/feed.xml only. Literal text is matched exactly. |
[] |
/docs/{...path} |
/docs/a, /docs/a/b/c (not /docs) |
['path' => 'a/b/c'] |
- Parameters are written
{name}. A name starts with a letter or_, then letters, digits and_, up to 32 characters. - A parameter matches one or more characters inside one path segment. It never matches
/or an empty string. - A segment can combine several parameters and literal text. Parameters must be separated by literal text, so
{a}{b}is rejected. When a segment could split in more than one way, earlier parameters take the shortest possible value:{x},{y}matching1,2,3givesx=1andy=2,3. - Catch-all
{...name}must be the whole last segment. It matches one or more remaining segments, and its value is the decoded segments joined with/. - A pattern must start with
/. Empty segments (//), unbalanced braces, invalid or duplicate names, adjacent parameters, and a catch-all that isn't the last segment all throwInvalidArgumentException.
Matching rules
- Query string and fragment (
?…,#…) are ignored. - Trailing slash: a single trailing slash is ignored, in both the URI and the pattern, so
/about/matches/about. Repeated slashes (/about//,/a//b) never match. - Case: paths are compared case-sensitively.
- Decoding: the URI is split on
/first, then each segment is percent-decoded exactly once withrawurldecode. A URI is not found when a decoded segment is.or.., or contains/(from%2F),\or a control character such as NUL, so segment boundaries can never change and no captured value can carry a path separator. Captured values are returned decoded (/users/J%C3%B3zefgivesid = "Józef"). Static segments are compared in decoded form, so/caf%C3%A9matches the pattern/café. Invalid escapes such as%zzare kept as they are.+is not a space. - Methods:
HEADfalls back toGETroutes unless aHEADroute is registered.any()routes match every method.
Precedence
Matching doesn't depend on registration order. Routes are compared segment by segment, left to right, and the first difference decides:
- A static segment beats a parameter segment.
- Among parameter segments, fewer parameters win, then more literal characters win. So
article-{id}beats{id}, and{article}.htmlbeats{slug},{id}.html. - A catch-all segment loses to everything else.
If the routes are still equal, a method-specific route beats a GET fallback for HEAD, which beats any(). After that the pattern text decides (byte order), so the result is deterministic.
Testing
composer install
composer test
Philosophy
- Small is Beautiful.
- Do one thing,
- Do one thing well.
- Get the PoC out ASAP.
- Consider your code dead two weeks after inception.
- n'joy!
sounds familiar? It should!