josantonius / router
此包已被废弃且不再维护。没有建议的替代包。
处理路由的库。
1.1.2
2022-08-18 21:06 UTC
Requires
- php: ^5.6 || ^7.0
- josantonius/url: ^1.1.9
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.3 || ^2.8
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^5.7 || ^6.0
- squizlabs/php_codesniffer: ^3.0
README
处理路由的库。
此文档针对版本 1.1.1。版本 1.1.2(归档了仓库)中的更改未记录或测试。
要求
此库支持 PHP 版本 5.6 或更高版本,并与 HHVM 版本 3.0 或更高版本兼容。
安装
安装此扩展的首选方式是通过 Composer。
要安装 PHP 路由库,只需
composer require Josantonius/Router
前面的命令只会安装必要的文件,如果您希望 下载整个源代码,可以使用
composer require Josantonius/Router --prefer-source
您还可以使用 Git 克隆完整仓库
$ git clone https://github.com/Josantonius/PHP-Router.git
或者 手动安装
下载 Router.php 和 Url.php
wget https://raw.githubusercontent.com/Josantonius/PHP-Router/master/src/Router.php
wget https://raw.githubusercontent.com/Josantonius/PHP-Url/master/src/Url.php
可用方法
此库中的可用方法
- 设置方法名称以使用单例模式
Router::setSingletonName($method);
属性 | 描述 | 类型 | 必需 | 默认 |
---|---|---|---|---|
$method | 单例方法名称。 | string | 是 |
返回 (boolean)
- 添加路由
Router::add($routes);
属性 | 键 | 描述 | 类型 | 必需 | 默认 |
---|---|---|---|---|---|
$routes | 要添加的路由。 | array | 是 | ||
0 | 路由。 | string | 是 | ||
1 | 方法 'class@method'。 | string | 是 |
返回 (boolean)
- 从 URI 获取要调用的方法
Router::getMethod($route);
属性 | 描述 | 类型 | 必需 | 默认 |
---|---|---|---|---|
$route | 路由。 | string | 是 |
返回 (string|null) → 路由或 null
- 如果路由未找到,则定义回调
Router::error($callback);
属性 | 描述 | 类型 | 必需 | 默认 |
---|---|---|---|---|
$callback | 回调。 | callable | 是 |
返回 (boolean true)
- 匹配后继续处理或停止它
也可以指定要处理的总路由数。
Router::keepLooking($value);
属性 | 描述 | 类型 | 必需 | 默认 |
---|---|---|---|---|
$value | 值。 | boolean | int | 是 |
返回 (boolean true)
- 为给定的请求运行回调
Router::dispatch();
返回 (call response|false)
快速入门
使用此库与 Composer 一起
require __DIR__ . '/vendor/autoload.php'; use Josantonius\Router\Router;
或者如果您已手动安装,请使用它
require_once __DIR__ . '/Router.php'; require_once __DIR__ . '/Url.php'; use Josantonius\Router\Router;
用法
- 添加路由
Router::add([ 'services' => 'Josantonius\Router\Example@services' ]);
- 添加路由
$routes = [ 'services' => 'Josantonius\Router\Example@services', 'home' => 'Josantonius\Router\Example@home', ]; Router::add($routes);
- 执行模拟 'services' 的路由
Router::dispatch(); // Response from services method
- 使用正则表达式 (:all) 添加路由
Router::add([ 'blog/:all' => 'Josantonius\Router\Example@blog' ]);
- 执行模拟 'language/PHP/' 的路由
Router::dispatch(); // Response from services method
- 使用正则表达式 (:any) 和参数添加路由
Router::add([ 'blog/:any/:any/' => 'Josantonius\Router\Example@blog', ]);
- 执行模拟 'blog/games/Minecraft/' 的路由
Router::dispatch(); // Response from blog method: games | Minecraft
- 使用正则表达式 (:num) 和参数添加路由
Router::add([ blog/:any/:num/' => 'Josantonius\Router\Example@blog', ]);
- 执行模拟 'blog/development/1/' 的路由
Router::dispatch(); // Response from blog method: development | 1
- 使用正则表达式 (:hex) 和参数添加路由
Router::add([ 'blog/:any/:hex/' => 'Josantonius\Router\Example@blog', ]);
- 执行模拟 'blog/color/e0a060/' 的路由
Router::dispatch(); // Response from blog method: color | e0a060
- 使用正则表达式 (:uuidV4) 和参数添加路由
Router::add([ 'blog/:any/:uuidV4/' => 'Josantonius\Router\Example@blog', ]);
- 执行模拟 'blog/uuid/11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000/' 的路由
Router::dispatch(); // Response from blog method: uuid | 11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000
- 设置方法名以使用单例模式
Router::setSingletonName('newSingleton');
- 获取方法
Router::getMethod('services'); // Josantonius\Router\Example@services
- 如果路由未找到,定义回调函数
Router::error('Josantonius\Router\Example@error');
- 使用自定义错误回调函数执行错误的路由
Router::dispatch('unknown'); // Response from error method
- 匹配后继续处理
Router::keepLooking();
- 持续查找最多三个匹配项
Router::keepLooking(3);
- 匹配后停止处理
Router::keepLooking(false);
测试
要运行测试,您只需要composer和执行以下命令:
git clone https://github.com/Josantonius/PHP-Router.git
cd PHP-Router
composer install
使用PHPUnit运行单元测试
composer phpunit
composer phpcs
使用PHP Mess Detector测试以检测代码风格的不一致性
composer phpmd
运行所有之前的测试
composer tests
赞助
如果这个项目帮助您减少了开发时间,您可以赞助我以支持我的开源工作 😊
许可协议
此存储库受MIT许可协议的许可。
版权所有 © 2016-2022,Josantonius