super-simple/router
最新稳定版本:v1.1
Composer 安装命令:
composer require super-simple/router
包简介
Super Simple PHP Routing library.
README 文档
README
PHP router library
Install
composer require super-simple/router
Requires PHP 8.1 or newer.
Usage
Basic usage:
// Create a router. $router = new Router; // Add a route $router->addRoute( '/test', // uri HTTPMethod::from($httpMethod), // http method function () {return 'success';} // handler in this case is callable ); // Or add a route as array $router->addRoute( '/test', // uri HTTPMethod::from($httpMethod), // http method [ 'controller' => SomeController::class, 'action' => 'index', ] // handler in this case array with config ); // Or create a config and add routes using config. The config requires keys: uri,method,handler // It depends on developer how to construct the handler array. $config = [ 'uri' => '/just-a-test-route', 'method' => 'GET', 'handler' => [ 'controller' => SomeController::class, 'action' => 'index', ], ]; $router->addConfig($config); ...... // Parse upcoming request $result = $router->parse($uri, HTTPMethod::from($httpMethod)); // $uri and $httpMethod must come from Request.
The result of parsing is an array.
The first element is the uri.
The second element is handler.
The third element contains the params if any exist.
$result[0]; // is the uri. // if the handler is callable you can call it like: $result[1](); // or with params $result[1](...$result[2]); // when using handler array instead of callable: // It depends on structure of handler array. // In this example we have two keys the controller and the action. (new $result[1]['controller'])->{$result[1]['action']}(); // Or with params (new $result[1]['controller'])->{$result[1]['action']}(...$result[2]);
You can expect some exceptions:
SSRouter\\NotFound // when router is not found \ValueError // when method not found in HTTPMethod enum \TypeError // comes from HTTPMethod enum or when the config array is not build correctly. // Simple use try { //....... } catch {} // to handle these errors.
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-24