evgsavosin/choco-router
最新稳定版本:2.1.1
Composer 安装命令:
composer require evgsavosin/choco-router
包简介
Modern router for PHP based on regex expression with cache system.
关键字:
README 文档
README
Modern router for PHP based on regex expression with cache system. Caching has driver support: file system, APCum, Memcached. Defining routes is possible both using methods and attributes from PHP 8.0.
Requirements
- PHP 8.1 or newer;
- APCu (optional);
- Memcached (optional).
Install
Install via composer:
composer require evgsavosin/choco-router
Usage
Basic usage
To use it is necessary to define the call of classes using for example PSR-11 implementation.
<?php declare(strict_types=1); require 'vendor/autoload.php'; use ChocoRouter\SimpleRouter; use ChocoRouter\HttpMethod; $router = new SimpleRouter(); $router->addRoute(HttpMethod::GET, '/foo', fn (): string => 'Foo!' ); $router->addRoute( HttpMethod::POST, '/foo/{bar}', fn (mixed $value): string => "Foo bar and {$value}!", ['bar' => '[0-9]+'] ); try { $router->resolve( $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'] )->callableResolve(function (mixed $handler, array $arguments): mixed { if (is_string($handler)) { [$controllerName, $methodName] = explode('@', $handler); // PSR-11 implementation for classes: controllers, actions and etc... } elseif (is_callable($handler)) { $handler(...$arguments); } }); } catch (HttpException $e) { if ($e->getCode() === HttpException::NOT_FOUND) { // Handle 404... } else if ($e->getCode() === HttpException::BAD_REQUEST) { // Handle 400... } }
Route definition
The route can be defined with method: addRoute(HttpMethod $httpMethod, string $uri, mixed $handler, array $parameters = []): void. Parameters can be passed {bar} or {bar?} with regular expressions ['bar' => '[0-9]+'].
Real example:
$router->addRoute(HttpMethod::GET, '/foo/{bar?}', 'foo-bar', ['bar' => '[0-9]+']);
A question mark means the parameter is optional.
The route group is defined using addGroup(string $prefix, callable $callback): void method. Real example:
$router->addGroup('/gorge', function (RouteCollection $r): void { $router->addRoute(HttpMethod::GET, '/foo/{bar?}', 'foo-bar', ['bar' => '[0-9]+']); });
HTTP Methods
Full list of methods:
HttpMethod::CONNECT HttpMethod::HEAD HttpMethod::GET HttpMethod::POST HttpMethod::PUT HttpMethod::DELETE HttpMethod::OPTIONS
Configuration
You can set the configuration when initializing a simple router:
$router = new SimpleRouter([ 'cacheDisable' => false, 'cacheDriver' => FileDriver::class, 'cacheOptions' => [] /* For memcached driver, there passed array of servers. For file driver, there passed path to cache directory. */ ]);
Attributes
The router supports attributes from PHP 8.0. Example:
use App\Action\FooAction; $router = new SimpleRouter(); $router->load([FooAction::class]); $router->resolve(/*...*/)->callableResolve(/*...*/);
Cache
Router has support cache system with defined drivers:
ChocoRouter\Cache\Drivers\FileDriver::class;ChocoRouter\Cache\Drivers\ApcuDriver::class;ChocoRouter\Cache\Drivers\MemcachedDriver::class.
For use cache move definition routes to cache callback:
$router = new SimpleRouter([ 'cacheDriver' => FileDriver::class ]); $router->cache(static function (RouteCollection $r): void { $r->addRoute(HttpMethod::GET, '/foo/{bar}', App\Actions\FooAction::class, ['bar' => '[0-9]+']); }); $router->resolve(/*...*/)->callableResolve(/*...*/);
Contributing
The author has not yet had time to write instructions, but any pull request or issue will be glad.
License
Choco Router has MIT License.
evgsavosin/choco-router 适用场景与选型建议
evgsavosin/choco-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「php」 「router」 「memcached」 「apcu」 「fast-route」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 evgsavosin/choco-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 evgsavosin/choco-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 evgsavosin/choco-router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Write down your routing mapping at one place
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
Router
PMVC App Action Router
Database alias router extension for Nette Framework
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-05