路由分组
路由分组
路由分组功能允许把相同前缀的路由定义合并分组,这样可以提高路由匹配的效率,不必每次都去遍历完整的路由规则。
例如,我们有定义如下两个路由规则的话
"blog/:id" => ["Blog/read", ["method" => "get"], ["id" => "d+"]],
"blog/:name" => ["Blog/read", ["method" => "post"]],
可以合并到一个blog分组
"[blog]" => [
":id" => ["Blog/read", ["method" => "get"], ["id" => "d+"]],
":name" => ["Blog/read", ["method" => "post"]],
],
可以使用Route
类的group
方法进行注册,如下:
Route::group("blog",[
":id" => ["Blog/read", ["method" => "get"], ["id" => "d+"]],
":name" => ["Blog/read", ["method" => "post"]],
]);
可以给分组路由定义一些公用的路由设置参数,例如:
Route::group("blog",[
":id" => ["Blog/read", [], ["id" => "d+"]],
":name" => ["Blog/read", [],
],["method"=>"get","ext"=>"html"]);
支持使用闭包方式注册路由分组,例如:
Route::group("blog",function(){
Route::any(":id","blog/read",[],["id"=>"d+"]);
Route::any(":name","blog/read",[],["name"=>"w+"]);
},["method"=>"get","ext"=>"html"]);
如果仅仅是用于对一些路由规则设置一些公共的路由参数,也可以使用:
Route::group(["method"=>"get","ext"=>"html"],function(){
Route::any("blog/:id","blog/read",[],["id"=>"d+"]);
Route::any("blog/:name","blog/read",[],["name"=>"w+"]);
});
路由分组支持嵌套,例如:
Route::group(["method"=>"get","ext"=>"html"],function(){
Route::group("blog",function(){
Route::any("blog/:id","blog/read",[],["id"=>"d+"]);
Route::any("blog/:name","blog/read",[],["name"=>"w+"]);
}
});