pinoven / routing
Pinoven 路由组件。管理路由就像它一样简单。
dev-master
2020-08-31 22:41 UTC
Requires
- php: >= 7.2.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-29 05:22:29 UTC
README
Pinoven Routing
安装
$ composer require pinoven/routing
特性/用法
接口
该组件提供了一套接口
// Describe how you can create router from settings. \Pinoven\Routing\Route\RouteFactoryInterface::class; // Describe element that have to be in a route and nothing else. \Pinoven\Routing\Route\RouteInterface::class; // Describe element that have to be in a router and nothing else. \Pinoven\Routing\Router\RouteInterface::class; // Should be use by the router to match a route. \Pinoven\Routing\Router\RouteMatcherInterface::class;
路由
定义
$controller = new class { public function hellWord() { } public function newRoute() { } }; $route = new \Pinoven\Routing\Route\Route('/my-route/{test}', [$controller, 'helloWord']); $routeWithAlias = new \Pinoven\Routing\Route\Route('/my-next-route/{test1}/{test2}', [$controller, 'newRoute'], 'route-alias');
您可以使用 getter/setter 来更改这些值。
您可以通过使用此来添加/更新该路由上的属性
$controller = new class { public function helloWord() { } }; $route = new \Pinoven\Routing\Route\Route('/my-route/{test}', [$controller, 'helloWord']); $route->setAttributes('priority', 4);
工厂
通过使用数组,您可以创建一个路由
use Pinoven\Routing\Route\RouteFactory; $controller = new class { // ... Controller definition }; $routeFactory = new RouteFactory(); $config = [ 'path' => '/hello/{name}', 'alias' => 'mon-alias', 'destination' => [$controller, 'methodToCall'], 'attributes' => [ 'priority' => 6, 'enabled' => false, ] ]; $route = $routeFactory->configure($config);
匹配器
路由匹配器是路由器将用来找到是否有可以绑定的一个或多个路由的东西。
use Pinoven\Routing\Route\RouteFactory; $controller = new class { // ... Controller definition }; $routeFactory = new RouteFactory(); $config = [ 'path' => '/hello/{name}', 'alias' => 'mon-alias', 'destination' => [$controller, 'methodToCall'] ]; $route = $routeFactory->configure($config); // Default expressions that can be used as delimiter for attributes. $defaultExpressions = [ new \Pinoven\Routing\Router\RouteExpression\BracesRouteExpression(), new \Pinoven\Routing\Router\RouteExpression\DigitBracesRouteExpression() ]; $matcher = new \Pinoven\Routing\Router\RouteMatcher($defaultExpressions); // $routeRequestData can be a RouteRequest or whatever you want to use to check if the route match with routing request. $routeRequestData = new \Pinoven\Routing\Router\RouteRequest\RouteRequest('https://www.test.com/test'); // It will return an empty array or with attributes or null if nothing matches. $matcher->match($routeRequestData, $route);
路由器
// Default expressions that can be used as delimiter for attributes. $defaultExpressions = [ new \Pinoven\Routing\Router\RouteExpression\BracesRouteExpression(), new \Pinoven\Routing\Router\RouteExpression\DigitBracesRouteExpression() ]; $matcher = new \Pinoven\Routing\Router\RouteMatcher($defaultExpressions); $router = new \Pinoven\Routing\Router\Router($matcher); // Default route matcher can be change $routeMatcher = new class implements \Pinoven\Routing\Router\RouteMatcherInterface { public function getRouteExpressions() : array{ } public function match($routeData,\Pinoven\Routing\Route\RouteInterface $route) : ?\Pinoven\Routing\Router\RouteRequest\RouteResultInterface{ } }; // must implement RoutMatcherInterface $router->setMatchRouteStrategy($routeMatcher);
路由结果
与路由请求匹配的路由将被封装到 RouteResult 实例中。它实现了
\Pinoven\Routing\Router\RouteRequest\RouteResultInterface::class;
贡献
路由方法(add、remove、find、findOne、get)应使用策略。我们在这里可以实施任何我们想要的东西,例如集合。
贡献
- 创建问题:改进 + 应该实现它的原因或问题 + 如何重现。
- 创建拉取请求并解释问题。
关于如何在所有 pinoven 包上贡献的更多信息将陆续提供。