attribute-router/router
Composer 安装命令:
composer require attribute-router/router
包简介
AttributeRouter is a lightweight and intuitive routing solution for PHP applications
README 文档
README
composer require attribute-router/router
Controller
declare(strict_types=1); namespace Controller; use AttributeRouter\Route; use AttributeRouter\RouteGroup; #[RouteGroup(path: '/admin', priority: 5)] class HomeController { #[Route( path: '/edit/{id}/{uuid?}', methods: ['GET'], name: 'edit', patterns: ['id' => '[0-9]+'], priority: 10 )] public function edit(int $id, ?string $uuid = null) { // } }
Initialization
require '../vendor/autoload.php'; use Controller\HomeController; use AttributeRouter\Router; use DI\Container; use Controller\TestController; $container = new Container; $router = $container->get(Router::class); $router->registerRoutes([ HomeController::class, TestController::class, ]); $requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $requestMethod = $_SERVER['REQUEST_METHOD']; $router->dispatch($requestUri, $requestMethod); $router->invokeController();
It is also possible to specify languages. But it should be specified before registering the controllers. In this case, the parameter /{locale?}/ will be automatically added to the beginning of all routes. A question mark means that this is an optional parameter. For example in the route /blog/123, would mean the default language, route /fr/blog/123, the French version.
$container = new Container; $router = $container->get(Router::class); $router->setDefaultLocale('en')->setLocales(['en', 'ru', 'fr']); $router->registerRoutes([ HomeController::class, TestController::class, ]); $requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $requestMethod = $_SERVER['REQUEST_METHOD']; $router->dispatch($requestUri, $requestMethod); $router->invokeController();
Cached routes can be forwarded. This method is shown for familiarization. You can use special classes for this.
$container = new Container; $router = $container->get(Router::class); $router->registerRoutes([ HomeController::class, TestController::class, ]); file_put_contents('cache.txt', serialize($router->getRoutes()));
If routes are cached, you can do without registering controllers
$container = new Container; $router = $container->get(Router::class); $data = unserialize(file_get_contents('cache.txt')); $router->setRoutes($data); $router->dispatch($requestUri, $requestMethod); $router->invokeController();
Current route
var_dump($router->getCurrent());
All routes
var_dump($router->getRoutes());
Register a new named parameter
$router->setAlias(alias: 'username', pattern: '[a-zA-Z0-9-_]+');
Get all named parameter
$router->getAliases();
attribute-router/router 适用场景与选型建议
attribute-router/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 attribute-router/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 attribute-router/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 32
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2024-11-04