Home> php教程> php手册> body text

分享一个PHP项目或者框架可用的路由类Router.class.php

WBOY
Release: 2016-06-06 19:39:38
Original
1277 people have browsed it

功能描述: 这个路由类本来是我自己框架里的一个核心类文件,因为我想听听大家的看法和建议,所以才放出来,如果有什么建议的请直接说把,欢迎吐槽。Router.class.php是一个实现url参数打包的php类文件,可用于基于mvc架构设计的项目,本类并未做异常处理,

功能描述:
这个路由类本来是我自己框架里的一个核心类文件,因为我想听听大家的看法和建议,所以才放出来,如果有什么建议的请直接说把,欢迎吐槽。Router.class.php是一个实现url参数打包的php类文件,可用于基于mvc架构设计的项目,本类并未做异常处理,使用者可以自行扩展

参数说明:
$url_mode:url模式,0 动态传参模式 例如:www.ruanpower.com/index.php?m=home&c=index&a=test&aid=5 1 pathinfo模式 例如:www.ruanpower.com/home/index/test/aid/5
$var_module: 模块变量名 例如:m、module
$var_controller:控制器变量名  例如:c、controller
$var_action: 方法变量名 例如: a、action

使用说明:
Router::init(array $config); //传入参数进行初始化
$param=Router::makeUrl() //makeUrl方法执行,完成参数打包

/** * 框架路由类 * * @author mgckid */ class Router { static private $url_mode; static private $var_controller; static private $var_action; static private $var_module; /** * 初始化方法 * @param type $config */ static public function init($config) { self::$url_mode = $config['URL_MODE']; self::$var_controller = $config['VAR_CONTROLLER']; self::$var_action = $config['VAR_ACTION']; self::$var_module = $config['VAR_MODULE']; } /** * 获取url打包参数 * @return type */ static public function makeUrl() { switch (self::$url_mode) { //动态url传参 模式 case 0: return self::getParamByDynamic(); break; //pathinfo 模式 case 1: return self::getParamByPathinfo(); break; } } /** * 获取参数通过url传参模式 */ static private function getParamByDynamic() { $arr = empty($_SERVER['QUERY_STRING']) ? array() : explode('&', $_SERVER['QUERY_STRING']); $data = array( 'module' => '', 'controller' => '', 'action' => '', 'param' => array() ); if (!empty($arr)) { $tmp = array(); $part = array(); foreach ($arr as $v) { $tmp = explode('=', $v); $tmp[1] = isset($tmp[1]) ? trim($tmp[1]) : ''; $part[$tmp[0]] = $tmp[1]; } if (isset($part[self::$var_module])) { $data['module'] = $part[self::$var_module]; unset($part[self::$var_module]); } if (isset($part[self::$var_controller])) { $data['controller'] = $part[self::$var_controller]; unset($part[self::$var_controller]); } if (isset($part[self::$var_action])) { $data['action'] = $part[self::$var_action]; unset($part[self::$var_action]); } switch ($_SERVER['REQUEST_METHOD']) { case 'GET': unset($_GET[self::$var_controller], $_GET[self::$var_action], $_GET[self::$var_module]); $data['param'] = array_merge($part, $_GET); unset($_GET); break; case 'POST': unset($_POST[self::$var_controller], $_POST[self::$var_action], $_GET[self::$var_module]); $data['param'] = array_merge($part, $_POST); unset($_POST); break; case 'HEAD': break; case 'PUT': break; } } return $data; } /** * 获取参数通过pathinfo模式 */ static private function getParamByPathinfo() { $part = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $data = array( 'module' => '', 'controller' => '', 'action' => '', 'param' => array() ); if (!empty($part)) { krsort($part); $data['module'] = array_pop($part); $data['controller'] = array_pop($part); $data['action'] = array_pop($part); ksort($part); $part = array_values($part); $tmp = array(); if (count($part) > 0) { foreach ($part as $k => $v) { if ($k % 2 == 0) { $tmp[$v] = isset($part[$k + 1]) ? $part[$k + 1] : ''; } } } switch ($_SERVER['REQUEST_METHOD']) { case 'GET': unset($_GET[self::$var_controller], $_GET[self::$var_action]); $data['param'] = array_merge($tmp, $_GET); unset($_GET); break; case 'POST': unset($_POST[self::$var_controller], $_POST[self::$var_action]); $data['param'] = array_merge($tmp, $_POST); unset($_POST); break; case 'HEAD': break; case 'PUT': break; } } return $data; } }
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!