介绍
用地址调用指定类内的方法并传入参数
代码
<?php
//路由类
class Routing
{
private $query, $path_array, $class, $method;
public function __construct($server)
{
$this->query = $server['QUERY_STRING'];
$path = $server['PATH_INFO'];
$path = array_filter(explode('/', $path));
$this->class = array_shift($path);
$this->method = array_shift($path);
$this->path_array=$path;
}
public function getquert()
{
parse_str($this->query, $result);
return $result;
}
public function getclass()
{
return $this->class;
}
public function getmethod()
{
return $this->method;
}
public function getpath()
{
for ($i = 0; $i < count($this->path_array); $i += 2) {
$value = $this->path_array[$i + 1];
if ($value) $result[$this->path_array[$i]] = $value;
}
return $result;
}
}
//控制器
class Controller
{
public function show($array)
{
foreach ($array as $key => $value) {
$result.=$key.'=>'.$value.'<br>';
}
return $result;
}
}
//创建路由对象
$a = new Routing($_SERVER);
//解析调用的类
$class=$a->getclass();
///解析调用的方法
$method=$a->getmethod();
///解析方法内的参数
$path=$a->getpath();
//生成解析的类
$b=new $class;
//调用类内的方法
echo $b->$method($path);
//http://php.edu/index.php/Controller/show/ffff/5435/vcd/356/fds/5455