300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > PHP DIY系列之自定义配置和路由

PHP DIY系列之自定义配置和路由

时间:2022-09-14 19:52:05

相关推荐

PHP DIY系列之自定义配置和路由

后端开发|php教程

php,自定义配置,路由

后端开发-php教程

安卓手机ip源码,Ubuntu的HDMI输入,tomcat7软件作用,nike鞋子爬虫,php中assign,黄浦区seo网络推广哪里好lzw

我们已经开发完成,但我们还需要更多。比如自定义配置和路由。

祝福网页源码,vscode js 缩进,vfsftp ubuntu,配置tomcat失败,sqlite3新特性,html5进度条插件 传递参数,为什么前端这么多框架,八条腿小爬虫,do while php,seo排名新技术,军事网站模板免费下载,python程序 自动填写网页,电商平台前台页面模板下载lzw

app文件夹下新建Config.php

择居网源码,vscode 注释 插件,ubuntu移,tomcat调用java,adt怎么连接sqlite,织梦插件调用,前端框架生命周期原理,网络爬虫python代码开源,php密码正则,陕西seo优化教程,企业标准网站模板,discuz网页斗地主,tp5网页模板映谢lzw

false, oute => [ \ => demo/welcome, est => demo/test, ],];

新建DemoController(app/Https/Controllers目录下)

response->json([hello => welcome]); } public function test($params) { return $this->response->json($params); }}

修改入口文件index.php,加入加载配置代码:

... 省略代码// 加载配置$config = require SF_LIBRARY_PATH.Config.php;$appConfig = file_exists($appConfigPath = SF_APP_PATH.Config.php) ? require $appConfigPath : [];$config = array_merge($config, $appConfig);$config[debug] = ($config[debug]?? SF_DEBUG);...省略代码

解析路由部分也加入自定义路由处理:

// Application...省略代码public function handleRequest(Request $request){ $route = $request->resolve($this->_config[ oute]??[]); $response = $request->runAction($route); /*** 执行结果赋值给$response->data,并返回给response对象*/ if ($response instanceof Response) { return $response; } throw new SaiException(Content format error);} ...省略代码 public function resolve($route=[]) {$this->route = $route; // 自定义路由return $this->getPathUrl(); } // Request ...省略代码public function runAction($route){ if (array_key_exists($route, $this->_route)) { $route = $this->_route[$route]; } $match = explode(/, $route); $match = array_filter($match); ...省略代码

保存后打开浏览器看看效果:

这里虽然有自定义路由,但是我们有时候需要禁止默认路由,所以我们不妨增加配置参数defaultRoute,用来控制是否开启默认路由。

我们修改一下路由解析的代码:

//Application...省略代码public function handleRequest(Request $request){ $route = $request->resolve($this->_config[ oute]??[]); $response = $request->runAction($route, $this->_config[defaultRoute]??true); /*** 执行结果赋值给$response->data,并返回给response对象*/ if ($response instanceof Response) { return $response; } throw new SaiException(Content format error);} ...省略代码

...省略代码public function runAction($route, $defaultRoute){ if (array_key_exists($route, $this->_route)) { $route = $this->_route[$route]; } elseif (!$defaultRoute) { throw new NotFoundException("route not found:".$route); } ...省略代码

我们在app下面的Config,加入:

return [ debug => false, oute => [ \ => demo/welcome, est => demo/test, ], defaultRoute => false,];

我们打开浏览器输入/login

报错如下:

Array( [line] => 137 [msg] => route not found:login [code] => 404 [file] => library/Https/Request.php)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。