路由分派(引荐进修: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写路由有几种要领的细致内容,更多请关注ki4网别的相干文章!