search
Homephp教程php手册简单的url路由控制[PHP]

简单的url路由控制[PHP]

Jun 06, 2016 pm 07:34 PM
definephpurlcontentcontrolSimpledetailedrouting

无详细内容 无 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools