exgalibas / gin
基于yii2的简单路由
1.0
2017-04-19 08:05 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 11:32:26 UTC
README
基于yii2的PHP简单路由。您可以通过下载文件或使用composer轻松获取源代码。
Composer
如果您有composer
composer require exgalibas/gin
使用
使用命名空间
use \Exgalibas\Gin\Gin;
您可以添加指定HTTP方法的路由规则,如GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|ANY
(new Gin)->get($pattern, $route)
模式
模式可以是普通字符串,带有名称的格式化正则表达式或原始正则表达式,例如
模式中的名称将在有<name>时用于替换路由,或者作为参数传递
// formatting regex
(new Gin)->get("<function:(post|comment)>/<action:(create|update|delete)>/<id:(\d+)>", "<function>")
此规则仅匹配 "exgalibas/gin/login",并调用名为 "login" 的函数
//ordinary string
(new Gin)->get("exgalibas/gin/login", "login")
仅是普通正则表达式,匹配如 "post/create/10",并调用名为 "create" 的函数
//original regular
(new Gin)->get("(post|comment)\/(create|delete)\/\d+", "create")
路由
路由可以是闭包、函数名或格式化字符串,如 "class|function"
示例
use \Exgalibas\Gin\Gin; $route = new Gin(); //example, match get url "post/10/create", call_user_func(["post", "create"], ["id" => 10]) $route->get("<class:(post|comment)>/<id:(\d+)>/<function:(create|update|delete)>", '<class>|<function>'); //delete route rule $route->deleteRule("<class:(post|comment)>/<id:(\d+)>/<function:(create|update|delete)>"); //example, match post url "exgalibas/login/10", call closure function([id=>10]) $route->post("exgalibas/login/<id:(\d+)>", function($params){...}) //example, match get url "exgalibas/login/joker", call login(["name" => "joker"]) $route->get("exgalibas/login/<name:joker>", "login") //parse the request $route->dispatch();
错误
如果派发错误,输出 "404"