Last active
December 17, 2015 09:49
-
-
Save chen7897499/5590398 to your computer and use it in GitHub Desktop.
用url中获取控制器和方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$C = array( | |
'URL_MODE' => 2, //URL模式, 1普通模式, 2 PATH_INFO模式 | |
'DEFAULT_CONTROL' => 'welcome', //默认调用的控制器 | |
'DEFAULT_ACTION' => 'index', //默认执行的方法 | |
); | |
class Controller | |
{ | |
protected function Analysis() | |
{ | |
global $C; //包含全局配置数组, 这个数组是在Config.php文件中定义的 | |
if($C['URL_MODE'] == 1) //如果URL模式为1 那么就在GET中获取控制器, 也就是说url地址是这种的http://localhost/index.php?c=控制器&a=方法 | |
{ | |
$control = !empty($_GET['c']) ? trim($_GET['c']) : ''; //trim() 函数从字符串的两端删除空白字符和其他预定义字符 | |
$action = !empty($_GET['a']) ? trim($_GET['a']) : ''; | |
} | |
else if($C['URL_MODE'] == 2) //如果为2 那么就是使用PATH_INFO模式, 也就是url地址是这样的 http://localhost/index.php/控制器/方法/其他参数 | |
{ | |
if(isset($_SERVER['PATH_INFO'])) | |
{ | |
//$_SERVER['PATH_INFO']URL地址中文件名后的路径信息, 不好理解, 来看看例子 | |
//比如你现在的URL是 http://www.php100.com/index.php 那么你的$_SERVER['PATH_INFO']就是空的 | |
//但是如果URL是 http://www.php100.com/index.php/abc/123 | |
//现在的$_SERVER['PATH_INFO']的值将会是 index.php文件名称后的内容 /abc/123/ | |
$path = trim($_SERVER['PATH_INFO'], '/'); | |
$paths = explode('/', $path); //explode — 使用一个字符串分割另一个字符串 返回字符串数组 | |
$control = array_shift($paths); //array_shift — 将数组开头的单元移出数组 返回移出的单元 $action = array_shift($paths); | |
} | |
} | |
//这里判断控制器的值是否为空, 如果是空的使用默认的 | |
$_GET['c'] = $control = !empty($control) ? $control : $C['DEFAULT_CONTROL']; | |
//和上面一样 | |
$_GET['a'] = $action = !empty($action) ? $action : $C['DEFAULT_ACTION']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment