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 High-performance radix tree based HTTP request router
License MIT
Homepage https://github.com/wilaak/radix-router
Informations about the package radix-router
Just a fast and simple HTTP request router for PHP. No regex!
RadixRouter (or RadXRouter) is a lightweight HTTP routing library focused on providing the essentials while being fast and small. It can make a good choice for simple applications or as the foundation for building your own custom more featureful router (see third-party integrations).
It features fast $O(k)$ dynamic route matching ($k$ = segments in path), path parameters (optional, wildcard; one per segment), simple API for listing routes/methods (for OPTIONS support), 405 method not allowed handling and it's all in a single file weighing in at around 400 lines of code with no external dependencies.
RadixRouter ranks as one of the fastest PHP routers. To see how this router compares to other common implementations in routing performance see the benchmarks section.
Install
It's only a single file that you can easily vendor on your own. If you want to user composer:
Requires PHP 8.0 or newer.
Example
Below is an example to get you started using the classic PHP SAPI.
Route Configuration
Routes are matched in a predictable order, always favoring the most specific pattern. Handlers can be any value you choose. In these examples, we use strings for simplicity, but you’re free to use arrays with extra details like middleware or other metadata.
If you plan to cache your routes your handlers must be exportable (see route caching). This router does not support regex patterns and it's recommended that you handle this logic in your handlers instead.
Path Parameters
Extract segments from the request path by specifying named placeholders in your route pattern.
Required
Matches only when the segment is present and not empty.
Optional
Match regardless of whether the segment is present. Only allowed in the trailing segments of a pattern.
[!TIP]
Use sparingly! In most cases you’re probably better off using query parameters instead of restricting yourself to a single filtering option in the path.
Wildcard
Also known as catch-all, splat, greedy, rest, or path remainder parameters; wildcards capture everything after their position in the path, including slashes. Because of this they must be used as the final segment.
[!CAUTION]
Never use captured path segments directly in filesystem operations. Path traversal attacks can expose sensitive files or directories. Use functions likerealpath()and restrict access to a safe base directory.
Prefix Matching
A wildcard at the end of a pattern matches everything under a prefix.
[!NOTE]
Wildcards don't capture a trailing slash, it's trimmed before matching. Restore it from the request path if you need it, e.g. when proxying. See trailing slashes.
Route Listing
Retrieve registered routes and their associated handlers. Optionally pass a request path to filter results to routes matching that path.
Example Output:
Allowed Methods
Retrieve the allowed HTTP methods for a given request path.
Route Caching
Route caching improves performance for traditional PHP deployments where scripts are reloaded on every request. In these environments, caching routes in a PHP file allows OPcache to keep them in shared memory, reducing script startup time by eliminating the need to recompile route definitions on each request.
PHP’s engine automatically interns identical string literals at compile time. This means that when multiple routes share the same pattern, method or handler name, only a single instance of each unique string is stored in memory, reducing memory usage and access latency.
For persistent environments such as Swoole, where the application and its routes remain in memory between requests, route caching is generally unnecessary. However you may still gain some performance uplift from the aforementioned interning.
Custom HTTP methods
You can add custom HTTP methods to the router, just make sure the method names are uppercase.
If you want a route to match any HTTP method (including custom ones), use the fallback method:
Trailing Slashes in URLs
This router does not perform any automatic trailing slash redirects. Trailing slashes at the end of the request path are automatically trimmed before route matching, so both /about and /about/ will match the same route.
When a route is successfully matched, the lookup method returns the canonical pattern of the matched route.
Important note on HEAD requests
By specification, servers must support the HEAD method for any GET resource, but without returning an entity body. In this router HEAD requests will automatically fall back to a GET route if you haven’t defined a specific HEAD route.
If you’re using PHP’s built-in web SAPI, the entity body is removed for HEAD responses automatically. If you’re implementing a custom server outside the web SAPI be sure not to send any entity body in response to HEAD requests.
Integrations
These are third-party so evaluate and use them at your own discretion. If you want or don't want your package listed here please create an issue or pull request.
| Package | Maintainer |
|---|---|
| Mezzio Framework | sirix777 |
| Yii Framework | sirix777 |
Benchmarks
Each path gets 1-3 HTTP methods, lookups follow a Zipf-like distribution, so a few hot routes dominate traffic.
- PHP 8.5.4
- AMD Ryzen AI 7 PRO 350 w/ Radeon 860M
- Seed: 42
License
This library is licensed under the MIT License.