Download the PHP package devlibs/routing without Composer
On this page you can find all versions of the php package devlibs/routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download devlibs/routing
More information about devlibs/routing
Files in devlibs/routing
Package routing
Short Description a fast and flexible HTTP Router
License BSD-3-Clause
Homepage https://github.com/devlibs/routing
Informations about the package routing
PHP Router
A fast, flexible and scalable HTTP router for PHP.
Features
- Grouping and nested group
- Easy to design RESTful API
- Full Tests
- Flexible and scalable
- No third-party library dependencies
- Named Param Placeholder
- Detect all request methods of the specify path
- Straightforward documentation
Requirements
- PHP -
7.0,7.1,7.2andmasterare tested.
Install
Documentation
Register handler
method-stringorarray, case-sensitive, such asGET,GET|POST(split by|, without space),['GET', 'POST']path- the path MUST start with slash/, such as/,/users,/users/<username>.handler-mixed, whatever you want.settings- user-defined settings.
Examples
| Method | Path | Handler | Matched | Unmatched |
|---|---|---|---|---|
GET |
/ |
handler | GET / |
POST / get / |
GET|POST |
/users |
handler | GET /users POST /users |
|
['GET', 'POST'] |
/merchants |
handler | GET /merchants POST /merchants |
|
GET |
/users/<username> |
handler | GET /users/foo GET /users/bar |
|
GET |
/orders/<order_id:\d+> |
handler | GET /orders/123456 |
GET /orders/letters |
It also provides a few shortcuts for registering handler:
Router::deleteRouter::getRouter::postRouter::put
Dispatch request
method- request method, case-sensitive.path- URI path
If matched, an RouteInterface will be returns, null otherwise.
Route
Class Route implements RouteInterface, provides some basic methods.
You can also define your own Route class via the following code snippet:
RouteInterface
Route class MUST implements this interface, see RouteInterface for more detail.
Named Params Placeholder
As the examples shown above, Router has ability to detect the param's value of the path.
In general, an placeholder pattern MUST be one of <name> and <name:regex>, it will be
converted to ([^/]+) and (regex) respectively.
You can also change it via replace the Router::$replacePatterns and Router::$replacements.
| Pattern | Path | Matched | Params |
|---|---|---|---|
/guests/<name> |
/guests/小明 |
YES | ['name' => '小明'] |
/guests/<name:\w+> |
/guests/foo |
YES | ['name' => 'foo'] |
/guests/<name:\w+> |
/guests/小明 |
NO | |
/orders/<order_id:\d+> |
/orders/123 |
YES | ['order_id' => '123'] |
/orders/<order_id:\d+> |
/orders/letters |
NO | |
/posts/<year:\d{4}>/<month:\d{2}>/<title> |
/posts/2017/10/hello-world |
YES | ['year' => '2017', 'month' => '10', title' => 'hello-world'] |
/posts/<year:\d{4}>/<month:\d{2}>/<title> |
/posts/201/10/hello-world |
NO | |
/posts/<year:\d{4}>/<month:\d{2}>/<title> |
/posts/2017/9/hello-world |
NO | |
/posts/<year:\d{4}><month:\d{2}>/<title> |
/posts/201710/hello-world |
YES | ['year' => '2017', 'month' => '10', title' => 'hello-world'] |
Settings
You can extend Router via settings, such as param's default value and middleware etc, but this topic are out of
scope of this document.
Grouping
Grouping is an powerful feature of Router for separating modules or API's versions. And this library also implements this feature, it allows nested grouping.
prefix- group prefix, it MUST NOT contains slash/.settings- settings for extending, it will inherits parent's settings.
RESTful API
As the examples shown above, it is obviously easy to design a RESTful API application.
Detect methods
In consideration of OPTIONS request, it provides an API for detecting all valid methods of the specify URI path.
path- request URL pathmethods-Router::$methodsdefined some common request methods, but it does not include all request methods, you can specify the methods if the key method is not one of theRouter::$methods.
FAQ
Package Not Found
Please add the following repository into repositories when composer complains about
that Could not find package devlibs/routing ....