leeoniya / route66
PHP 微型路由器
dev-master
2022-01-27 17:27 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-28 17:12:32 UTC
README
PHP 微型路由器 (MIT 许可证)
- 简洁、简单的语法,包含完整的功能集
- 易于安装;Composer 或单个文件包含 (~200 行代码)
- 低路由开销 (< 1ms)*
Route66 是为了在不使路由器膨胀的情况下,向最小化路由器 添加一些功能 而编写的。
Nginx 配置示例
server { index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?/$uri; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
安装(要求或 Composer)
require 'Route66.php';
https://packagist.org.cn/packages/leeoniya/route66
{
"require": {
"leeoniya/route66": "dev-master"
}
}
基本示例
// front controller (index.php) require 'Route66.php'; use Route66 as R; R::get('/', function() { echo 'Hello world!'; }); R::dispatch();
HTTP 方法
// any HTTP method can be bound via static invocation using its name R::post('/topics', function() {}); // multiple methods R::match('post|put', '/comment', function() {}); // equivalent to R::match('get|post|put|patch|delete|head|options',..) R::any('/', function() {};)
命名参数 & 验证
// basic param (all characters except '/') R::get('/posts/@id', function($id) {}); // param validated by regex alias R::get('/posts/@id:alpha', function($id) {}); // aliases and regexs can be broken out for readability R::get('/posts/@id', function($id) {}, ['id' => ':alpha']); R::get('/posts/@id', function($id) {}, ['id' => '\w{12}']); // define a custom alias R::alias(':date', '[0-9]{4}-[0-9]{2}-[0-9]{2}');
可选段 & 参数
// optional params with defaults (set in handler) R::get('/posts(/@year(/@month(/@day)))', function($year = 2015, $month = 6, $day = 15) {}); // regex alias without a param (non-capturing) R::get('/posts(/:slug)', function() {}); // optional trailing slash (though rtrim-ing it from REQUEST_URI before dispatch is faster) R::get('/blog/?', function() {});
未命名参数
// un-named params via regex capture groups R::get('/posts/(\w{12})', function($id) {});
路由前缀(基本路径)
R::base('/blog'); R::get('/posts', function() {}); // maps to /blog/posts R::base('/admin'); R::get('/login', function() {}); // maps to /admin/login
在预过滤器之前(穿透)
// before all /admin* routes R::get('/admin(/:all)', function() { // verify valid session, etc... return R::NOHALT; // fall through to additional routes }); R::get('/admin/dashboard', function() { // show dashboard... });
自定义通配符(未找到路由)
R::nomatch(function($meth, $uri) { header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); exit('404 Not Found.'); });
自定义分配(例如内部重定向)
R::dispatch('get', '/blog');
路由缓存
// example of storing compiled routes in session session_start(); if ($routes = @$_SESSION['routes']) R::import($routes); // define all routes here R::dispatch(); if (!isset($_SESSION['routes'])) $_SESSION['routes'] = R::export();
命名路由 & 反向路由
// un-named R::get('/users/@id', function($id) {}, ['id'=>':alpha']); // named (the trailing 2 args can be passed in any order. all below are equivalent.) R::get('/users/@id', function($id) {}, 'named1'); R::get('/users/@id', function($id) {}, ['id'=>':alpha'], 'named1'); R::get('/users/@id', function($id) {}, 'named1', ['id'=>':alpha']); // dispatch a named route R::dispatch('get', 'named1', ['id' => 'abc123']);
实用方法
R::is_ajax(); R::is_https(); R::redirect($location, $code = 301);
新功能(相对于 Macaw)
- 命名参数(例如
@user:alpha) - 可选路由段和参数,参数默认值
- 路由组前缀
- 参数正则表达式 & 别名可以分离开来以提高可读性
- 多方法路由的简短定义
- 命名路由和反向路由,自定义分配
- 每个路由的穿透控制(Macaw 只有全局的)
- 路由编译缓存,导入/导出
- 用于
is_ajax、is_https和redirect的实用方法 - 修改和添加的正则表达式别名
其他 PHP 路由器
- Macaw - https://github.com/NoahBuscher/Macaw
- Phroute - https://github.com/mrjgreen/phroute
- Klein - https://github.com/chriso/klein.php
- FastRoute - https://github.com/nikic/FastRoute
- Pux - https://github.com/c9s/Pux
- AltoRouter - https://github.com/dannyvankooten/AltoRouter
- Aura - https://github.com/auraphp/Aura.Router
- mu - https://github.com/lastguest/mu
注:与任何路由器一样,速度将取决于:路由数量和复杂度、参数数量、是否使用缓存、服务器硬件、PHP 版本、指令缓存。