phapi/middleware-route
最新稳定版本:1.0.2
Composer 安装命令:
composer require phapi/middleware-route
包简介
Middleware handling routing and dispatching to endpoints.
README 文档
README
The Route Middleware package contains two middleware: Phapi\Middleware\Route\Route and Phapi\Middleware\Route\Dispatch that handles routing and dispatch to endpoints.
The Route Middleware will invoke the first route that matches the current HTTP request’s URI and method. If the request URI doesn't match any defined route a 404 Not Found will be returned to the client. If the request method isn't implemented a 405 Method Not Allowed will be returned instead.
The router does three things to be able to find a match, first it tries to find a direct match. This is possible for routes that doesn't contain regular expressions. If no direct match can be found the router will look in it's cache for a match. As a last resort the router tries to match against the routes that contains regular expressions. If a match is found it will be added to the cache.
Installation
This middleware is by default included in the Phapi Framework but if you need to install it it's available to install via Packagist and Composer.
$ php composer.phar require phapi/middleware-route:1.*
Configuration
The configuration contains three steps:
- Create middleware objects
- Define routes
- Add to pipeline
Create middleware objects
<?php use Phapi\Middleware\Route\Route; use Phapi\Middleware\Route\Router; use Phapi\Middleware\Route\RouteParser; use Phapi\Middleware\Route\Dispatch; // Use the dependency injection container to get the configured cache $route = new Route(new Router(new RouteParser, $container['cache']));
Define routes
Routes can be added by passing an array (with the route as the key and the name of the resource as the value) to the addRoutes function.
<?php // Create a list of routes $routes = [ '/users' => '\\Phapi\\Endpoint\\Users', '/users/{name:a}' => '\\Phapi\\Endpoint\\User', '/articles/{id:[0-9]+}' => '\\Phapi\\Endpoint\\Article', '/blog/{slug}/{title:c}?' => '\\Phapi\\Endpoint\\Blog\\Post', ]; $route->addRoutes($routes);
By default a route pattern syntax is used where {foo} specified a placeholder with name foo and matching the string [^/]+. To adjust the pattern the placeholder matches, you can specify a custom pattern by writing {bar:[0-9]+}.
Regex Shortcuts
:i => :/d+ # numbers only :a => :[a-zA-Z0-9]+ # alphanumeric :c => :[a-zA-Z0-9+_-\.]+ # alnumnumeric and +-_. characters :h => :[a-fA-F0-9]+ # hex
Get params in an Endpoint
The router will inject the params in to the called method on the Endpoint. Lets use this route as an example:
<?php $routes = [ '/users/{name:a}' => '\\Phapi\\Endpoint\\User', ];
If we want to get the name value from /users/phapi we need to specify an endpoint looking like this:
<?php class User extends Endpoint { public function get($name) // $name will contain 'phapi' { ... } }
Add to pipeline
Last but not least, add the middleware to the pipeline. The Dispatcher doesn't need any configuration.
<?php $pipeline->pipe($route); $pipeline->pipe(new \Phapi\Middleware\Route\Dispatcher());
Complete example
<?php use Phapi\Middleware\Route\Route; use Phapi\Middleware\Route\Router; use Phapi\Middleware\Route\RouteParser; use Phapi\Middleware\Route\Dispatch; // Use the dependency injection container to get the configured cache $route = new Route(new Router(new RouteParser, $container['cache'])); // Create a list of routes $routes = [ '/users' => '\\Phapi\\Endpoint\\Users', '/users/{name:a}' => '\\Phapi\\Endpoint\\User', '/articles/{id:[0-9]+}' => '\\Phapi\\Endpoint\\Article', '/blog/{slug}/{title:c}?' => '\\Phapi\\Endpoint\\Blog\\Post', ]; $route->addRoutes($routes); $pipeline->pipe($route); $pipeline->pipe(new \Phapi\Middleware\Route\Dispatcher());
See the configuration documentation for more information about how to configure the integration with the Phapi Framework.
Phapi
This middleware is a Phapi package used by the Phapi Framework. The middleware are also PSR-7 compliant and implements the Phapi Middleware Contract.
License
The Route Middleware is licensed under the MIT License - see the license.md file for details
Contribute
Contribution, bug fixes etc are always welcome.
phapi/middleware-route 适用场景与选型建议
phapi/middleware-route 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.55k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「routing」 「router」 「dispatcher」 「middleware」 「dispatch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phapi/middleware-route 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phapi/middleware-route 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phapi/middleware-route 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
A Laravel-like router for the WordPress Rewrite API
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
A Laravel helper to detect if the current route/path is active.
Router
统计信息
- 总下载量: 1.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-29