opensid/router
Composer 安装命令:
composer require opensid/router
包简介
Improved routing, middleware support, authentication tools and more for CodeIgniter 3 framework
README 文档
README
OpenSID CI is an awesome set of core improvements for CodeIgniter 3 that makes the development of APIs (and websites in general) more easy!
Features
- Easy installation via hooks
- Laravel-like routing: prefixes, namespaces, anonymous functions as routes, route groups, CLI routes, named parameters, optional parameters, etc.
- Middleware support
- PHP Debug Bar integration (experimental)
Requirements
- PHP >= 5.6.0 (PHP 7 compatible)
- CodeIgniter >= 3.0
Installation
Step 1: Get OpenSID CI with Composer
composer require OpenSID/OpenSID
Step 2: Enable Hooks and Composer autoload
<?php # application/config/config.php $config['enable_hooks'] = TRUE; $config['composer_autoload'] = TRUE;
Step 3: Connect OpenSID CI with CodeIgniter
Set the hooks:
<?php # application/config/hooks.php defined('BASEPATH') OR exit('No direct script access allowed'); // (...) $hook = OpenSID\Hook::getHooks();
Set the OpenSID CI routes:
<?php # application/config/routes.php defined('BASEPATH') OR exit('No direct script access allowed'); // (...) $route = OpenSID\Route::getRoutes();
Initialization
The first time that OpenSID CI runs, several files and folders are created:
routes/web.php: Default HTTP-Based routesroutes/api.php: AJAX routesroutes/cli.php: CLI routescontrollers/OpenSID.php: Fake controller, necessary to use some routesmiddleware: Middleware folder
Important: Make sure that your application folder has write permission!
Usage
To add routes, use the static methods of the Route class:
<?php # application/routes/web.php // This points to 'baz' method of 'bar' controller at '/foo' path under a GET request: Route::get('foo', 'bar@baz'); // To add a route parameter, enclose with curly brackets {} Route::get('blog/{slug}', 'blog@post'); // To make a parameter optional, add a ? just before closing the curly brackets // (OpenSID CI will make all the fallback routes for you) Route::get('categories/{primary?}/{secondary?}/{filter?}', 'clients@list'); // The (:any) and (:num) CodeIgniter route placeholders are available to use, with this syntax: Route::get('cars/{num:id}/{any:registration}', 'CarCatalog@index'); // Custom regex? it's possible with this syntax: Route::post('main/{((es|en)):_locale}/about', 'about@index');
Use the Route::cli() method to add command line routes. All CLI routes must be inside routes/cli.php file. This is an example of a CLI route:
Route::cli('path','controller@method');
The ci() function returns the framework instance, acting as a virtual controller. This is useful if you use callbacks as routes:
Route::get('foo', function(){ ci()->load->view('some_view'); });
You can assign names to your routes so you don't have to worry about future url changes:
Route::get('company/about_us', 'testcontroller@index')->name('about_us');
To retrieve a named route, use the route() function:
<a href="<?= route('about_us');?>">My link!</a> // <a href="http://example.com/company/about_us">Link</a>
If the route has parameters, pass a second argument to the function with an array of their values:
<?= route('route_name', ['param1' => 'value2', 'param2' => 'value2' ... ]); ?>
Route Groups
Group routes is possible with the Route::group() method. All routes
inside the group will share the prefix (first argument) and, optionally, another property (namespace, middleware, etc.)
// Prefix only Route::group('prefix', function(){ Route::get('bar','test@bar'); Route::get('baz','test@baz'); }); // Prefix and shared properties Route::group('prefix', ['namespace' => 'foo', 'middleware' => ['Admin','IPFilter']], function(){ Route::get('bar','test@bar'); Route::get('baz','test@baz'); });
Middleware
All the middleware must be defined in the routes files (application/routes/web.php, application/routes/api.php, application/routes/cli.php)
# Route middleware: Route::put('foo/bar','controller@method', ['middleware' => ['Test']]); # Route group middleware: Route::group('site', ['middleware' => ['Admin']], function(){ // ... }); # Global middleware: Route::middleware('Admin', 'pre_controller');
The middleware files must be saved in the application/middleware folder. If not exists, you must create it first. A middleware file is any php class that implements the OpenSID\MiddlewareInterface interface and with a public run() method, which is the entry point. It's strongly advised to name all your middleware with CamelCase and avoid name conflicts with your controllers.
This is an example of a middleware:
<?php # application/Middleware/TestMiddleware.php class TestMiddleware { public function run() { // (Entry point) } }
Documentation
Visit the project official website (disponible en español)
opensid/router 适用场景与选型建议
opensid/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.85k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「codeigniter」 「middleware」 「OpenSID」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 opensid/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 opensid/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 opensid/router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
The CodeIgniter Redis package
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.
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 2.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-03