一个基础的PHP路由
composer require emerge/php-routing
use Emerge\Routing;
$routing = new Routing();
$routing->get(
'/admin',
['App\Admin', 'index']
);
$routing->get(
'/admin/user/$id',
['App\Admin', 'index'],
[
'requirements' => [
'id' => '\d+'
]
]
);
$result = $routing->dispatch('GET', '/admin/user/1');
print_r($result);
输出内容
Array
(
[route] => /admin/user/$id
[callback] => Array
(
[0] => App\Admin
[1] => index
)
[options] => Array
(
[requirements] => Array
(
[id] => \d+
)
[method] => Array
(
[0] => GET
)
)
[parameters] => Array
(
[0] => /admin/user/1
[id] => 1
[1] => 1
)
)
string
版本号
$routing::VERSION
array get
(string $route, array|callable $callback, [array $options = []])
返回添加的路由数据
route
路由路径
callback
路由回调,参考链接
options
路由选项, 支持 requirements,method 两个选项
Array
(
[route] => /admin/user/$id
[callback] => Array
(
[0] => App\Admin
[1] => index
)
[options] => Array
(
[requirements] => Array
(
[id] => \d+
)
[method] => Array
(
[0] => GET
)
)
)
$routing->get(
'/admin/user/$id',
['App\Admin', 'index'],
[
'method' => ['GET', 'POST']
'requirements' => [
'id' => '\d+'
]
]
);
array dispatch
(string $method, string $uri)
返回路由分派结果
method
HTTP方法
uri
URI
Array
(
[route] => /admin/user/$id
[callback] => Array
(
[0] => App\Admin
[1] => index
)
[options] => Array
(
[requirements] => Array
(
[id] => \d+
)
[method] => Array
(
[0] => GET
)
)
[parameters] => Array
(
[0] => /admin/user/1
[id] => 1
[1] => 1
)
)
$result = $routing->dispatch('GET', '/admin/user/1');