kenjis/ci4-attribute-routes
Composer 安装命令:
composer require kenjis/ci4-attribute-routes
包简介
CodeIgniter4 Attribute Routes module
关键字:
README 文档
README
This package generates a Routes File from the Attribute Routes in your Controllers.
- You can set routes in your Controllers, and disable Auto Routing.
- It generates a Routes File, so, there is no extra overhead at runtime.
- The generated Routes File can be used on PHP 7.3 production servers.
use Kenjis\CI4\AttributeRoutes\Route; class SomeController extends BaseController { #[Route('path', methods: ['get'])] public function index() { ... } }
Requirements
- CodeIgniter 4.3.1 or later
- Composer
- PHP 8.0 or later
Installation
$ composer require kenjis/ci4-attribute-routes
Configuration
- Add the following code to the bottom of your
app/Config/Routes.phpfile:
/* * Attribute Routes * * To update the route file, run the following command: * $ php spark route:update * * @see https://github.com/kenjis/ci4-attribute-routes */ if (file_exists(APPPATH . 'Config/RoutesFromAttribute.php')) { require APPPATH . 'Config/RoutesFromAttribute.php'; }
- Disable auto routing and enable route priority:
--- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -22,7 +22,8 @@ $routes->setDefaultController('Home'); $routes->setDefaultMethod('index'); $routes->setTranslateURIDashes(false); $routes->set404Override(); -$routes->setAutoRoute(true); +$routes->setAutoRoute(false); +$routes->setPrioritize();
This is optional, but strongly recommended.
Quick Start
1. Add Attribute Routes to your Controllers
Add #[Route()] attributes to your Controller methods.
<?php namespace App\Controllers; use Kenjis\CI4\AttributeRoutes\Route; class News extends BaseController { #[Route('news', methods: ['get'])] public function index() { ... } }
2. Update Routes File
$ php spark route:update
APPPATH/Config/RoutesFromAttribute.php is generated.
Check your routes with the php spark routes command.
Route Attributes
Route
#[Route('news', methods: ['get'])]
#[Route('news/create', methods: ['get', 'post'])]
#[Route('news/(:segment)', methods: ['get'], options: ['priority' => 1])]
RouteGroup
use Kenjis\CI4\AttributeRoutes\RouteGroup; #[RouteGroup('', options: ['filter' => 'auth'])] class GroupController extends BaseController { #[Route('group/a', methods: ['get'])] public function getA(): void { ... } ... }
RouteResource
use Kenjis\CI4\AttributeRoutes\RouteResource; #[RouteResource('photos', options: ['websafe' => 1])] class ResourceController extends ResourceController { ... }
RoutePresenter
use Kenjis\CI4\AttributeRoutes\RoutePresenter; #[RoutePresenter('presenter')] class PresenterController extends ResourcePresenter { ... }
Trouble Shooting
No routes in the generated routes file
You must import the attribute classes in your controllers.
E.g.:
use Kenjis\CI4\AttributeRoutes\Route; ... #[Route('news', methods: ['get'])] public function index()
Can't be routed correctly, or 404 error occurs
Show your routes with the php spark routes command, and check the order of the routes.
The first matched route is the one that is executed.
The placeholders like (.*) or ([^/]+) takes any characters or segment. So you have to move the routes like that to the bottom.
In one controller, you can move the methods having such routes to the bottom.
Or set the priority of the routes with options:
#[Route('news/(:segment)', methods: ['get'], options: ['priority' => 1])]
Zero is the default priority, and the higher the number specified in the priority option, the lower route priority in the processing queue.
For Development
Installation
composer install
Available Commands
composer test // Run unit test
composer tests // Test and quality checks
composer cs-fix // Fix the coding style
composer sa // Run static analysys tools
composer run-script --list // List all commands
kenjis/ci4-attribute-routes 适用场景与选型建议
kenjis/ci4-attribute-routes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 557 次下载、GitHub Stars 达 26, 最近一次更新时间为 2022 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「codeigniter4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kenjis/ci4-attribute-routes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kenjis/ci4-attribute-routes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kenjis/ci4-attribute-routes 相关的其它包
同方向 / 同关键字的高下载量 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.
JSON Web Token for codeigniter4 authentication.
ORM Database mapping for Codeigniter 4
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
A router designed for web requests for the Monolith framework
统计信息
- 总下载量: 557
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-18