您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php写路由有几种方法

2025/8/7 17:48:23发布19次查看
路由的功能就是分发请求到不同的控制器,基于的原理就是正则匹配。接下来呢,我们实现一个简单的路由器,实现的能力是对于静态的路由(没占位符的),正确调用callback。
路由分配(推荐学习:php视频教程)
基于php的路由分配,实质上来说就是利用url中的path去匹配对应的控制类,同时调用其中的方法进行相关操作的处理。
<?php// 权限控制include_once './auth.php';// 应用入口文件date_default_timezone_set("asia/shanghai");header('content-type: text/html;charset=utf-8');// 项目根路径define('basepath', dirname(__file__));// 调试模式define('app_debug', true);// 引入配置文件include_once basepath . '/config/config.php';// 路由控制$router = include_once basepath . '/config/router.php';if ($_server['http_host'] !== 'xxx.com') { var_dump('当前host不被允许');} else { $request_path = str_replace('/index.php', '', $_server['php_self']); $request_query = getcurrentquery(); if (array_key_exists($request_path, $router)) { $module_file = basepath . $router[$request_path]['file_name']; $class_name = $router[$request_path]['class_name']; $method_name = $router[$request_path]['method_name']; if (file_exists($module_file)) { include $module_file; $obj_module = new $class_name(); if (!method_exists($obj_module, $method_name)) { die("要调用的方法不存在"); } else { if (is_callable(array($obj_module, $method_name))) { $obj_module->$method_name($request_query, $_post); } } } else { die("定义的模块不存在"); } } else { echo '页面不存在'; }}
以上就是php写路由有几种方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product