Download the PHP package timetoogo/rapid-route without Composer
On this page you can find all versions of the php package timetoogo/rapid-route. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download timetoogo/rapid-route
More information about timetoogo/rapid-route
Files in timetoogo/rapid-route
Package rapid-route
Short Description Another fast router library for PHP
License MIT
Homepage http://www.github.com/TimeToogo/RapidRoute
Informations about the package rapid-route
RapidRoute - Another fast router for PHP
RapidRoute aims to be another fast router for PHP. This library takes a different approach to uri routing by compiling the router to optimized PHP code, minimizing the need for traditional regular expressions.
As this project focuses on performance, the scope of this library is limited. All in all, this library provides the ability to match a supplied HTTP request (method and uri) against a set of route definitions. See below for usage examples.
Benchmarks
Test Name | RapidRoute (req/sec) | FastRoute (req/sec) | Change |
---|---|---|---|
First static route | 3385.28 | 2906.64 | 16.47% faster |
Last static route | 3419.56 | 2901.09 | 17.87% faster |
First dynamic route | 3428.94 | 2829.18 | 21.20% faster |
Last dynamic route | 3379.56 | 2890.18 | 16.93% faster |
Non-existent route | 3412.31 | 2823.27 | 20.86% faster |
Longest route | 3371.36 | 2853.40 | 18.15% faster |
Invalid method, static route | 3125.81 | 2864.19 | 9.13% faster |
Invalid method, dynamic route | 3402.57 | 2847.55 | 19.49% faster |
These results are generated using this benchmark suite running on PHP 5.5 with opcache enabled. These results indicate a consistent 10-20% performance gain over FastRoute depending on the input uri and http method.
Installation
This project is compatible with PHP 5.4+. It can be loaded via composer:
Router Usage
This library is designed to be used by another library/framework or as a standalone package. It provides specific APIs for each use case.
Usage in a framework
A framework often provides its own wrapper API so this library offers a lower-level API in this case. A basic example is shown:
The result from the router is an array that contains the result status as the first element. The following elements of the array are dependent on the status and will be one of three formats:
Usage as a standalone package
If this library is intended to be used as a standalone package, a cleaner and more extensive wrapper API is provided. A similar example showing off this the API is shown:
The result from the call to $router->match(...)
will be an instance of RapidRoute\MatchResult
.
Route definitions
Route patterns
To define the routes, a familiar url structure is used:
Adding Routes
To define the routes, the router API takes a callable
parameter which will
be called with an instance of RapidRoute\RouteCollection
when the router is
being compiled. This can be used like so:
Using the RouteCollection
you can also define a route parameter regex globally
to avoid repetitions:
Basic usage example
The associated route data will be available when the route is matched. This is a very basic example of how this library may be implemented as a standalone router package. The route data contains the associated handler so it can be easily dispatched when the route is matched.
Here are some examples of how this set up should handle the incoming request:
Request | Dispatched Handler |
---|---|
GET / | HomeController::index([]) |
GET /user | UserController::index([]) |
POST /user | UserController::store([]) |
POST / | ErrorController::methodNotAllowed(['GET']) |
GET /abc | ErrorController::notFound() |
GET /user/123 | UserController::show(['user_id' => '123']) |
PUT /user/123 | UserController::update(['user_id' => '123']) |
PUT /user/abc | ErrorController::notFound() |
Notes
- When matching a uri, the uri string must contain a preceding
/
if it is not empty. - Route defined with a trailing slash will not match a uri without the slash
'/shop/product/'
will not match'/shop/product'
and vice-versa
- A route that allows the
GET
method will also accept theHEAD
method as per HTTP spec.
Compilation
Given that this library compiles route definitions to plain PHP, there is much
room for optimization. The current approach is using a tree structure matching
each segment in a uri ('/shop/product'
is composed of the 'shop'
and 'product'
segments).
Currently the structure is compiled to nested switch
and if
blocks using
optimized comparisons where applicable.
One consideration of the compiled router is that it must be able to be called directly and as such must handle the any expected error cases within the compiled router.
Example compiled router
Route definitions:
Currently the compiled router for the above will be similar to the following:
The complexity of the router will grow in proportion to the number and complexity of the route definitions.