samueltissot / wp_route
v0.3.0
2018-11-09 15:15 UTC
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-09-16 03:01:06 UTC
README
这是Anthony Budd的Anthony Budd的WP_Route的分支
新增功能
- 小的安全修复
- 命名路径变量
- 将参数重命名为PathVariable
- 将请求对象传递给可调用函数
- 忽略查询字符串的选项
基本用法
WP_Route::get('/flights/', 'listFlights'); WP_Route::get('/buses/', 'listBuses'); WP_Route::post('/flights/{flight}/', 'singleFlight'); WP_Route::put('/flights/{flight}/book/{date}', 'bookFlight'); WP_Route::delete('/flights/{flight}/delete', 'deleteFlight'); WP_Route::any('flights/{flight}', array('Class', 'staticMethod')); WP_Route::patch('flights/{flight}', array($object, 'method')); WP_Route::match(['get', 'post'], 'flights/{flight}/confirm', 'confirmFlight'); // if you want to take into account the parameters when doing a path match WP_Route::get('/flights/', 'listFlights', ["match" => "*"]); // if you want to match one or more parameters WP_Route::get('/flights/', 'listFlights', ["match" => ['param2', 'param2', ...]]); // redirect WP_Route::redirect('open-google', 'https://google.com', 301); // close WP_Route::get('flights/{flight}', function singleFlight(RequestInterface $req) { $req->pathVariable('flight'); }
参数列表
[
"parameters" => [
'match' => [[parameter1] [, parameter2 [, ...]]],
'no_match' => [[parameter1] [, parameter2 [, ...]]],
],
]
注意:空数组表示:应用于所有
安装
使用composer要求WP_Route
$ composer require samueltissot/wp_route
所有回调必须接受类型为 RequestInterface 的变量
请求对象传递给可调用方法
注意:计划能够提供您自定义的RequestInterface类(已接受PR)
示例
use samueltissot\WP_Route\RequestInterface; // an invocable class class Controller { public function __invoke(RequestInterface $req) { // code goes here; } } // or a simple function function my_super_func(RequestInterface $req) { // code goes here; } // method inside class class MyAwesomeClass { public function wow(RequestInterface $req) { // code goes here; } }
请求对象(RequestInterface)
一个小助手对象,提供有关请求的有用数据
接口
interface RequestInterface { public function uri(); public function method(); public function pathVariables(); public function pathVariable($name); public function parameters(); public function parameter($name); }