PHP Routing
, (*1)
A fast, flexible and scalable HTTP router for PHP., (*2)
Features
- Easy to design RESTful API
- Full Tests
-
Flexible and scalable: it allows you to define your own handler to deal with
Not Found, Method Not Allowed and OPTIONS request.
- 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.2 and master are supported.
Install
composer require razonyang/php-routing
Documentation
include '/path-to-vendor/autoload.php';
use RazonYang\Routing\Router;
// create an router instance
$router = new Router();
Register handler
Router::handle($method, $path, $handler);
-
method - string or array, such as GET, GET|POST(split by |, without spaces), ['GET', 'POST']
-
path - the path MUST start with slash /, such as /, /users, /users/<username>.
-
handler - mixed, whatever you want.
Examples, (*3)
| 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:, (*4)
Router::delete
Router::get
Router::post
Router::put
$router->get('/', 'handler');
$router->handle('GET|POST', '/users', 'handler');
$router->handle(['GET', 'POST'], '/merchants', 'handler');
$router->get('/users/<username>', 'handler');
$router->get('/orders/<order_id:\d+>', 'handler');
Dispatch request
Router::dispatch($method, $path);
-
method - request method.
-
path - URI path.
If matched, a Route instance will be returns, null otherwise or NotFoundException/MethodNotAllowedException will be thrown., (*5)
$path = '/users/baz';
$route = $router->dispatch(Router::METHOD_GET, $path);
// handle requset
$handler = $route->handler; // 'handler'
$params = $route->params; // ['username' => 'baz']
Named Params Placeholder
As the examples shown above, Router has ability to detect the param's value of the path., (*6)
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., (*7)
| 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'] |
RESTful API
As the examples shown above, it is obviously easy to design a RESTful API application., (*8)
$router->get('/products', 'products');
$router->post('/products', 'create product');
$router->get('/products/<product_id:\d+>', 'product detail');
$router->put('/products/<product_id:\d+>', 'update product');
$router->delete('/products/<product_id:\d+>', 'delete product');
Not Found Handler
$router->notFoundHandler = function($method, $path) {
throw new \Exception('404 Not Found');
};
Method Not Allowed Handler
$router->methodNotAllowedHandler = function($method, $path, $allowedMethods) {
throw new \Exception('405 Method Not Allowed');
};
OPTIONS Handler
$router->optionsHandler = function($method, $path, $allowedMethods) {
header('Allow: ' . implode(',', $allowedMethods));
};
FAQ
Package Not Found
Please add the following repository into repositories when composer complains about
that Could not find package razonyang/php-routing ...., (*9)
{
"type": "git",
"url": "https://github.com/razonyang/php-routing.git"
}