路由一:
<?php
//!从url 中解析出控制器、方法、参数
//!get 请求
$url ='http://string.com/0509/index.php?c=user&a=hello&id=22&name=wuyu';
class UserController{
static function hello($id = 1,$name='测试'){
return "Hello id=$id,name=$name";
}
}
parse_str($_SERVER['QUERY_STRING'],$params);
print_r($params);
array_filter($params);
$controller = ucfirst(array_shift($params)) . 'Controller';
$action = array_shift($params);
echo call_user_func_array([$controller,$action],$params);
路由二:主流框架大多用此方法
<?php
function p_array($requst){
printf('<pre>%s</pre>',print_r($requst,true));
}
//!从url 中解析出控制器、方法、参数
//!主流框架解决方法pathinfo
$url ='http://string.com/0509/index1.php/admin/user/index';
echo $_SERVER['PATH_INFO'] . '<br>';
// print_r(explode('/',ltrim($_SERVER['PATH_INFO'],'/')));
$requst = explode('/',ltrim($_SERVER['PATH_INFO'],'/'));
[$model,$controller,$action] = $requst;
printf('模块:%s,控制器:%s, 方法:%s,',$model,$controller,$action);
$className = $model . '\\' . ucfirst($controller);
echo $className . '<br>';
p_array($requst);
p_array(array_slice($requst,3));
$params = array_slice($requst,3);
$arr = array_chunk($params,2);
$result = [];
foreach($arr as $item){
[$key,$value] = $item;
$result[$key] = $value;
}
p_array($result);
$result = array_filter($result);