Home  >  Article  >  php教程  >  简单的url路由控制[PHP]

简单的url路由控制[PHP]

WBOY
WBOYOriginal
2016-06-06 19:34:411444browse

无详细内容 无 define('BASE_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);//系统根目录define('MODULE_PATH', BASE_PATH.'modules'.DIRECTORY_SEPARATOR);//模块目录 $module = empty($_GET['m']) ? '' : strtolower($_GET['m']); //文件夹$control = emp

define('BASE_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);//系统根目录
define('MODULE_PATH', BASE_PATH.'modules'.DIRECTORY_SEPARATOR);//模块目录
 
$module = empty($_GET['m']) ? '' : strtolower($_GET['m']); //文件夹
$control = empty($_GET['c'])? 'index' : strtolower($_GET['c']); //文件{类}
$action = empty($_GET['a'])? 'index' : strtolower($_GET['a']); //方法
 
if($module){
     $controlFile = MODULE_PATH . $module . DIRECTORY_SEPARATOR . $control . '.php';
     if( !file_exists($controlFile) )
          echo $module . DIRECTORY_SEPARATOR . $control . '.php'.'类文件不存在'; exit();
 
     include($controlFile);
     $c = new $control;
 
     if( !method_exists($c, $action) )
          echo $c.'方法不存在'; exit();
 
     $c->$action();
}else{
     //默认首页[可用smarty模板引擎加载视图]
     $smarty->display('index.html');
}
/**
写了个简单的路由控制功能,根据参数m,c,a,控制响应url的模块,文件,方法。(MVC)方式URL。
说明:
MVC方式,m模块[对应模块文件夹],c控制器[对应类文件],a方法[对应对象方法]
没有指定module时默认访问系统首页
没有指定control时默认访问index[文件|类],没有该文件时输出文件[类]不存在错误
没有指定action时默认访问index[方法],没有该方法时输出方法不存在错误

例如:http://www.xxx.com/web/index.php?m=admin
访问 module下admin文件夹下的index文件(类名与文件名相同)下的index方法
*/
Statement:
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
Previous article:获取在线ipNext article:php提取osc最近发动弹的人