Download the PHP package wilaak/radix-router without Composer
On this page you can find all versions of the php package wilaak/radix-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wilaak/radix-router
More information about wilaak/radix-router
Files in wilaak/radix-router
Package radix-router
Short Description Simple radix tree based HTTP request router for PHP.
License WTFPL
Homepage https://github.com/Wilaak/RadixRouter
Informations about the package radix-router
RadixRouter
Simple radix tree based HTTP request router for PHP. Lightweight and high-performance (see benchmarks)
Overview
- High-performance O(k) dynamic route matching, where k is the number of segments in the path.
- Supports parameters, including wildcard and optional segments for flexible route definitions.
- Static routes are stored in a hash map providing fast minimal allocation lookups for exact matches.
How does it work?
The router splits the path into segments and walks the tree, matching each segment in order. Because the tree only branches where routes differ, the router can quickly skip irrelevant routes and find the correct handler with minimal comparisons.
Install
Install with composer:
composer require wilaak/radix-router
Or simply include it in your project:
Requires PHP 8.0 or newer. (PHP 8.3 for tests)
Usage Example
Here's a basic usage example using the typical PHP-FPM (FastCGI) web environment:
Registering Routes
Routes are registered using the add()
method. You can assign any value as the handler. The order of route matching is: static > parameter.
Note: Trailing slashes are ignored. For example, both
/about
and/about/
are treated as the same route.
Below is an example showing the different ways to define routes:
How to Cache Routes
Rebuilding the route tree on every request or application startup can slow down performance.
Note: Anonymous functions (closures) are not supported for route caching because they cannot be serialized. When caching routes, only use handlers that can be safely represented as strings, arrays, or serializable objects.
Note: When implementing route caching, care should be taken to avoid race conditions when rebuilding the cache file. Ensure that the cache is written atomically so that each request can always fully load a valid cache file without errors or partial data.
Here is a simple cache implementation:
By storing your routes in a PHP file, you let PHP’s OPcache handle the heavy lifting, making startup times nearly instantaneous.
Note on HEAD Requests
According to the HTTP specification, any route that handles a GET request should also support HEAD requests. RadixRouter does not automatically add this behavior. If you are running outside a standard web server environment (such as in a custom server), ensure that your GET routes also respond appropriately to HEAD requests. Responses to HEAD requests must not include a message body.
Benchmarks
Single-threaded benchmark (Xeon E-2136, PHP 8.4.8 cli OPcache enabled):
Simple App (33 Routes)
Router | Register | Lookups | Memory Usage | Peak Memory |
---|---|---|---|---|
RadixRouter | 0.04 ms | 3,233,227/sec | 375 KB | 456 KB |
FastRoute | 1.85 ms | 2,767,883/sec | 431 KB | 1,328 KB |
SymfonyRouter | 6.24 ms | 1,722,432/sec | 574 KB | 1,328 KB |
Avatax API (256 Routes)
Router | Register | Lookups | Memory Usage | Peak Memory |
---|---|---|---|---|
RadixRouter | 0.25 ms | 2,127,808/sec | 587 KB | 588 KB |
FastRoute | 4.94 ms | 707,516/sec | 549 KB | 1,328 KB |
SymfonyRouter | 12.60 ms | 1,182,060/sec | 1,292 KB | 1,588 KB |
Bitbucket API (178 Routes)
Router | Register | Lookups | Memory Usage | Peak Memory |
---|---|---|---|---|
RadixRouter | 0.17 ms | 1,781,226/sec | 532 KB | 533 KB |
FastRoute | 3.81 ms | 371,104/sec | 556 KB | 1,328 KB |
SymfonyRouter | 12.16 ms | 910,064/sec | 1,186 KB | 1,426 KB |
License
This library is licensed under the WTFPL-2.0. Do whatever you want with it.