delolmo/symfony-router
Composer 安装命令:
composer require delolmo/symfony-router
包简介
PSR-15 middleware to use the symfony/routing component and store the route attributes in the request.
README 文档
README
PSR-15 middleware to use the symfony/routing component and store the route attributes in the request.
Requirements
- PHP >=8.4
- A PSR-7 http library
- A PSR-17 http factory
Installation
This package is installable and autoloadable via Composer as delolmo/symfony-router.
composer require delolmo/symfony-router
Example
Consider Symfony's PhpFileLoader to load route definitions from the following file:
# routes.php use App\Controller\BlogController; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $routes = new RouteCollection(); $routes->add('blog_list', new Route('/blog', array( 'request-handler' => [BlogController::class, 'list'] ))); $routes->add('blog_show', new Route('/blog/{slug}', array( 'request-handler' => [BlogController::class, 'show'] ))); return $routes;
For this example, we will be using middlewares/utils for a PSR-15 compliant
dispatcher. See link
for more PSR-15 implementations.
This example uses a basic anonymous function to print the route's attributes:
use Laminas\Diactoros\Response\HtmlResponse; use Laminas\Diactoros\ResponseFactory; use Laminas\Diactoros\ServerRequest; use Middlewares\Utils\Dispatcher; use Symfony\Component\Config\FileLocator; use Symfony\Component\Routing\Loader\PhpFileLoader; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\Router; $fileLocator = new FileLocator(array(__DIR__)); $router = new Router( new PhpFileLoader($fileLocator), 'routes.php', array('cache_dir' => __DIR__ . '/cache'), new RequestContext('/') ); $responseFactory = new ResponseFactory(); $dispatcher = new Dispatcher([ new DelOlmo\Middleware\SymfonyRouterMiddleware($router, $responseFactory), function($request, $next) { return new HtmlResponse(json_encode($request->getAttributes())); } ]); // Try matching a /blog request $response = $dispatcher->dispatch(new ServerRequest([], [], '/blog')); // Will return {"_route": "blog_list", "request-handler" => ["App\Controller\BlogController", "list"]} $c->get('emitter')->emit($response); // Try matching a /blog/hello-world request $response = $dispatcher->dispatch(new ServerRequest([], [], '/blog/hello-world')); // Will return {"_route": "blog_show", "request-handler" => ["App\Controller\BlogController", "show"], "slug" => "hello-world"} $c->get('emitter')->emit($response);
Options
The constructor takes two arguments:
__construct(
\Symfony\Component\Routing\Router $router,
\Psr\Http\Message\ResponseFactoryInterface $responseFactory
)
The router instance to use and a PSR-17 factory to create the error
responses (404 or 405).
delolmo/symfony-router 适用场景与选型建议
delolmo/symfony-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 105 次下载、GitHub Stars 达 7, 最近一次更新时间为 2018 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「http」 「router」 「server」 「middleware」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 delolmo/symfony-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 delolmo/symfony-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 delolmo/symfony-router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Router
PMVC App Action Router
Database alias router extension for Nette Framework
统计信息
- 总下载量: 105
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-01