opsway/slim-attribute-router
Composer 安装命令:
composer require opsway/slim-attribute-router
包简介
Slim attribute router with support of groups and middlewares
README 文档
README
Slim attribute router
This package allows you to add routes to your slim4 (https://www.slimframework.com) application using attributes.
Features
- Route methods support
- Route name support
- Route group support
- Middlewares support
Installation
This package can be installed using Composer
Navigate into your project's root directory and execute the bash command shown below
composer require opsway/slim-attribute-router
Enabling the Attribute Router
Attribute router extends slims default RouteCollector, all you need to do is instantiate attribute router and pass it on to AppFactory. First parameter is array of namespace parts of app. It will be used for filtering classes in which search of parameters will go on.
<?php use OpsWay\Slim\AttributeRouter\Router; use Slim\Factory\AppFactory; $route = new Router([ ['NameSpace'], // array of namespaces parts of app AppFactory::determineResponseFactory(), new CallableResolver($container) // optional DI container ); AppFactory::setRouteCollector($attributeRouteCollector); $app = AppFactory::create(); $app->run();
Attribute signature
#[Route({methods}, {path}[[, {group}][, {name}][, {isDeprecated}]])]
| Parameter | Example | Description |
|---|---|---|
| {methods} | ['GET', 'POST'] | (array) The allowed HTTP request methods |
| {path} | '/hello/{parameter}' | (string) The route pattern |
| {group} | 'group' | (string) The group name |
| {name} | 'route.Name' | (string) The name of the route |
| {isDeprecated} | 'false' | (bool) Flag if route is deprecated |
- The "methods" parameter is required and must be not empty
- The "path" parameter is required and must be not empty
- Rest of parameters are optional
#[Group({name} [[, {classes}]])]
| Parameter | Example | Description |
|---|---|---|
| {name} | '/api' | (string) The group name |
| {classes} | ['Middleware::class'] | (array) Middlewares class names |
- The "name" parameter is required and must be compatible with URI string requirements
- The "classes" parameter is optional. List of classes implementing Psr\Http\Server\MiddlewareInterface
#[Middlewares({firstClass} [, {secondClass}])]
| Parameter | Example | Description |
|---|---|---|
| {firstClass} | ['Middleware::class'] | (string) Middleware class name |
- The "firstClass" parameter is required and must class name of class that implements Psr\Http\Server\MiddlewareInterface
- The "secondClass" and all other parameters is optional.
- All arguments of constructor are converted to an array of middleware classes. It should be at least one middleware class
Specifying attributes examples
Example specifying route with name and group parameters at class level
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; #[Route([Route::METHOD_GET], '/api/hello-world', '/hello-world-group', 'api.hello-world.route-name')] class HelloWorld { public function __invoke(Request $request, Response $response): Response { // some php code return $response; } }
Example specifying route without additional parameters at method level
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; class HelloWorld { #[Route([Route::METHOD_GET], '/api/hello-world')] public function __invoke(Request $request, Response $response): Response { // some php code return $response; } }
Example specifying group with middlewares at class level
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; #[Group('/api', [FirstMidleware::class, SecondMidleware::class])] class HelloWorld { public function __invoke(Request $request, Response $response): Response { // some php code return $response; } }
opsway/slim-attribute-router 适用场景与选型建议
opsway/slim-attribute-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.93k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 opsway/slim-attribute-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 opsway/slim-attribute-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-03