收好100个最常用的PHP函数_后端开发
下面的列表是最常用的前100个PHP函数,它们是最常用的 PHP 中自带的函数,这些函数被命名使用,并从1到100进行排序,其他的4500个函数现在还没在排名中。
我们已经开发完成,但我们还需要更多。
比如自定义配置和路由。
app文件夹下新建Config.php
<?php/** *自定义配置 */return [ 'debug' => false, 'route' => [ '' => 'demo/welcome', 'test' => 'demo/test', ],];
新建DemoController
(app/Https/Controllers目录下)
<?php/** * Demo控制器 */namespace App\Https\Controllers;use Library\Https\Controller;class DemoController extends Controller{ public function welcome($params) { return $this->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['route']??[]); $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); ...省略代码
保存后打开浏览器看看效果:
PHP如何结合MySQL进行千万级数据处理_后端开发
一张一亿的订单表,可以分成五张表,这样每张表就只有两千万数据,分担了原来一张表的压力,分表需要根据某个条件进行分,这里可以根据地区来分表,需要一个中间件来控制到底是去哪张表去找到自己想要的数据。
这里虽然有自定义路由,但是我们有时候需要禁止默认路由,所以我们不妨增加配置参数defaultRoute,用来控制是否开启默认路由。
我们修改一下路由解析的代码:
//Application...省略代码 public function handleRequest(Request $request){ $route = $request->resolve($this->_config['route']??[]); $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, 'route' => [ '' => 'demo/welcome', 'test' => 'demo/test', ], 'defaultRoute' => false,];
我们打开浏览器输入saif.com/login
报错如下:
Array ( [line] => 137 [msg] => route not found:login [code] => 404 [file] => library/Https/Request.php )
相关学习推荐:PHP编程从入门到精通
以上就是PHP DIY系列之自定义配置和路由的详细内容,更多请关注ki4网其它相关文章!
\n\n在c语言中是什么意思?_后端开发
“\n\n”在c语言中表示连续两次换行。
“\n”是换行符,通常在输出中用作格式控制;“\n”就是一个转义字符,其意义是“回车换行”;转义字符以反斜线“\”开头,后面跟一个或几个字符。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)