[db:作者] 发表于 2025-7-26 12:52

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){case dispatcher::not _ found://请求的方法/...404 not foundthrow new routenotfoundException("请求的方法不存在:{break;case dispatcher::method _ not _ allowed://错误的请求类型$ allowed methods = $ route info;/...405方法不允许抛出新方法不允许异常(“请求类型错误({$httpMethod}),当前方法允许请求类型({ $ allowed methods})”);break;case Dispatcher::FOUND: //查找请求的方法:调用方法$handler = $routeInfo即可;$ vars = $ route info;call _ user _ func(,$handler],$ 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;$ 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),$routeItem,$ route item);} unset($ route item);}});} else {if($ route file){foreach($ route file as $ route item){$ router- gt;add route(strtoupper($ route item),$routeItem,$ route item);} unset($ route item);}}} unset($ route file);});}}
涉及异常类的编写
创建一个功能路由文件,(演示:route/web.php,route/api.php)
文件写入格式(可以自定义路由文件的make_dispatcher方法)
/',[['get ',' users ',[ AppControllertest Controller::class,' index']],]];
测试控制器
路由访问

因此,成功地引入了路由组件。
注:如果想拓展更多使用方法,请访问文章开头的github地址,查看更多使用方法。
页: [1]
查看完整版本: route实现路由功能route设置路由