logifire / nano-router
使用PSR-7实现的路由器实现
4.0.2
2021-10-05 17:49 UTC
Requires
- php: ^7.2
- psr/container: ^1.0.0 | ^2.0.1
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- baryshev/tree-route: ^2.0
- logifire/nano-container: ^2.0.3
- logifire/nano-middleware: ^0.2.2
- nyholm/psr7: 1.0.1
- phpunit/phpunit: ^9.5
Suggests
- logifire/nano-container: A PSR-11 container implementation, required for the middleware implementation.
- logifire/nano-middleware: A PSR-15 middleware implementation.
This package is auto-updated.
Last update: 2024-09-05 23:58:10 UTC
README
用法
基本
// Controller implementation
class StringController implements Controller {
public function run(): ResponseInterface {
...
}
}
...
// Router configuration
$router = new Router();
$router->configurePath(Router::METHOD_GET, '/profiles/(?<uuid>[0-9a-f\-]{36})', StringController::class);
$router->configurePath(Router::METHOD_GET, '/profiles/(?<id>\d+)', IntegerController::class);
...
// Request handling
$router_result = $router->processRequest($server_request);
if ($router_result !== null) {
$controller_name = $router_result->getControllerName();
$path_result = $router_result->getPathResult();
$query_result = $router_result->getQueryResult();
$controller = new $controller_name({dependencies});
$psr7_response = $controller->buildResponse();
}
...
此包包含PSR-15 RouterMiddleware