route实现路由功能route设置路由

【route实现路由功能route设置路由】分享给互联网从爱好者学习参考。

GitHub地址:https://github.com/nikic/FastRoute/tree/v1.3.0

nikic/fast-route包介绍#这次推出的是1.3.0版本的composer包 composer需要Nikic/fast-route v1.3.0。写路由配置加载方法dispatch($httpMethod,$ uri);switch($ route info[0]){case dispatcher::not _ found://请求的方法/...404 not foundthrow new routenotfoundException("请求的方法不存在:{break;case dispatcher::method _ not _ allowed://错误的请求类型$ allowed methods = $ route info[1];/...405方法不允许抛出新方法不允许异常(“请求类型错误({$httpMethod}),当前方法允许请求类型({ $ allowed methods[0]})”);break;case Dispatcher::FOUND: //查找请求的方法:调用方法$handler = $routeInfo[1]即可;$ vars = $ route info[2];call _ user _ func([new $ handler[0],$handler[1]],$ vars);break;}}私有静态函数make _ dispatcher($ route file list){return simple dispatcher(function(route collector $ router)use($ route file list){foreach($ route file list as $ route file){if(isset($ route file[' prefix ']){$ routerm routers = $ route file[0];$ router- gt;addGroup($routeFile['prefix'],function(route collector $ router)use($ routers){if($ routers){foreach($ routers as $ route item){$ router- gt;add route(strtoupper($ route item[0]),$routeItem[1],$ route item[2]);} unset($ route item);}});} else {if($ route file){foreach($ route file as $ route item){$ router- gt;add route(strtoupper($ route item[0]),$routeItem[1],$ route item[2]);} unset($ route item);}}} unset($ route file);});}}

涉及异常类的编写

创建一个功能路由文件,(演示:route/web.php,route/api.php)

文件写入格式(可以自定义路由文件的make_dispatcher方法)

/',[['get ',' users ',[ AppControllertest Controller::class,' index']],]];

测试控制器

路由访问

?

因此,成功地引入了路由组件。
注:如果想拓展更多使用方法,请访问文章开头的github地址,查看更多使用方法。

route实现路由功能route设置路由