kenphp/router
Composer 安装命令:
composer require kenphp/router
包简介
Simple PHP Router
README 文档
README
A simple PHP Router for your web application. This library is part of KenPHP Project, but can be used independently.
Features
- Static Route Patterns
- Dynamic Route Patterns
- Named parameters
- Optional parameters
- Regex-based route patterns
- Subrouting
- Supports web application routing
- Before and After route middleware
- Custom handler when a route is not found
What it does ?
- Store route patterns, handlers, and middlewares information
- Resolve request to matching patterns
What it doesn't ?
- Parse route path from $_SERVER or any other means. You must provide the route path and method to
Router::resolvemethod. - Execute middlewares and handlers. It only returns an array containing matched route handlers, middlewares, and parameters found in the request.
- Why isn't this library receives
Psr\Http\Message\RequestInterfaceimplementation and returnsPsr\Http\Message\ResponseInterface?
This library aims to gives as much freedom as possible to the user. Not everyone are using PSR-7 implementation and we want to respect that.
Requirements
- PHP 7.0 or greater
Installation
The easiest way to install is using Composer
$ composer require kenphp/router
Methods
-
route($method, $route, $handler, $options = []) : voidExample :
$router->route('GET', '/users', ['UserController', 'listUsers']);
-
get($route, $handler, $options = []) : voidExample :
$router->get('/users/{id}', ['UserController', 'getUser']);
-
head($route, $handler, $options = []) : voidExample :
$router->head('/users', ['UserController', 'listUsers']);
-
post($route, $handler, $options = []) : voidExample :
$router->post('/users', ['UserController', 'createUser']);
-
put($route, $handler, $options = []) : voidExample :
$router->put('/users/{id}', ['UserController', 'updateUser']);
-
delete($route, $handler, $options = []) : voidExample :
$router->delete('/users/{id}', ['UserController', 'deleteUser']);
-
group($route, $fn, $options = []) : voidExample :
$router->group('/api', function() use ($router) { $router->get('/products/{id}', ['ProductController', 'getProduct']); });
-
setNotFoundHandler(callable $handler) : voidExample :
$router->setNotFoundHandler(function() { echo 'Page not found.'; });
-
resolve($requestRoute, $method) : null|arrayThis function would return an array containing the following keys :
handlerparams- Optional keys. This would be filled with any data from the
$optionsparameter.
Example :
$router->get('/users/{id}', ['UserController', 'getUser'], [ 'namespace' => 'app\controllers' ]); $routeArray = $router->resolve('/users/1', 'GET'); /** * $routeArray would contains * [ * 'handler' => ['UserController', 'getUser'], * 'params' => ['id' => 1], * 'namespace' => 'app/controllers', * ] */
Examples
统计信息
- 总下载量: 64
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-06