codeinc / router
此包已被废弃且不再维护。没有建议的替代包。
Code Inc. PSR7 & PSR15 路由库
4.0.0
2018-11-07 16:00 UTC
Requires
- php: >=7.1
- codeinc/psr7-responses: ^2
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- codeinc/error-renderer: ^1.2
- codeinc/psr7-response-sender: ^1.1
- guzzlehttp/psr7: ^1.4
- phpunit/phpunit: ^7
Suggests
- codeinc/middleware-dispatcher: A PSR-15 middleware dispatcher
- codeinc/psr7-response-sender: A PSR-7 response sender
- codeinc/psr7-responses: Provides a collection of PSR-7 responses
- codeinc/router-annotation-resolver: A resolver using Doctrine's annotation system to detect and configure request handlers
- codeinc/router-routable-resolver: A resolver using interfaces to detect and configure request handlers
- dev-master
- 4.x-dev
- 4.0.0
- 3.x-dev
- 3.4.1
- 3.4.0
- 3.4.0-beta.2
- 3.4.0-beta.1
- 3.3.0
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.x-dev
- 2.3.1
- 2.3.1-beta.1
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 2.0.0-beta.5
- 2.0.0-beta.4
- 2.0.0-beta.3
- 2.0.0-beta.2
- 2.0.0-beta.1
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.12
- 1.3.11
- 1.3.10
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0-beta.1
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-beta.3
- 1.0.0-beta.2
- 1.0.0-beta.1
This package is auto-updated.
Last update: 2020-02-07 20:53:57 UTC
README
此库提供了一个兼容 PSR-7 和 PSR-15 的路由器。路由器是一个负责选择适当的控制器(实现 ControllerInterface
)的组件。它作为 PSR-15 中间件 (MiddlewareInterface
)运行。
解析器
为了选择控制器,路由器依赖于解析器(实现 ResolverInterface
)。解析器负责将 URI 路径(称为路由)与控制器匹配,反之亦然,将控制器映射到路由。提供了两种解析器,StaticResolver
,它使用预设的模式列表(由 fnmatch()
解析的 shell 模式)匹配处理器的类,以及 DynamicResolver
,它基于 PHP 命名空间和基本 URI 动态计算路由。您可以使用 ResolverAggregator
将多个解析器配对。外部包提供基于 Doctrine 的注解 或基于 接口 的解析器。
用法
基本用法
<?php use CodeInc\Router\Router; use CodeInc\Router\ControllerInterface; use CodeInc\Router\Resolvers\StaticResolver; // dummy classes final class MyInstantiator implements ControllerInterface {} final class HomePage implements ControllerInterface {} final class License implements ControllerInterface {} final class Article implements ControllerInterface {} // instantiating the router $myRouter = new Router( new StaticResolver([ '/' => HomePage::class, '/license.txt' => License::class, '/article-[0-9]/*' => Article::class ]) ); // controller lookup (assuming the URI of the request is "/article-2456/a-great-article.html") $myRouter->process($aPsr7ServerRequest, $aFallbackHandler); // <-- returns 'ArticleController'
使用多个解析器和多个实例化器
<?php use CodeInc\Router\Router; use CodeInc\Router\ControllerInterface; use Doctrine\Instantiator\InstantiatorInterface; use CodeInc\Router\Resolvers\ResolverAggregator; use CodeInc\Router\Resolvers\StaticResolver; use CodeInc\Router\Resolvers\DynamicResolver; // dummy classes final class MyFirstInstantiator implements InstantiatorInterface {} final class MySecondInstantiator implements InstantiatorInterface {} final class HomePage implements ControllerInterface {} final class License implements ControllerInterface {} final class Article implements ControllerInterface {} // instantiating the router $myRouter = new Router( new ResolverAggregator([ new StaticResolver([ '/' => HomePage::class, '/license.txt' => License::class, '/article-[0-9]/*' => Article::class ]), new DynamicResolver( 'MyApp\\MyHandlers', // <-- handlers base namespace '/my-app/' // <-- handlers base URI ) ]) ); // processing the response $myRouter->process($aPsr7ServerRequest, $aFallbackHandler);
安装
此库可通过 Packagist 获得,并可以使用 Composer 安装。
composer require codeinc/router
许可
此库在 MIT 许可下发布(请参阅 LICENSE
文件)。