批改状态:合格
老师批语:将路由参数解析出来的方式许多, 我只提供了一种, 你还可以用其它方式, 还有参数的解析, 这个玩意比方法难
用地址调用指定类内的方法并传入参数
<?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
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号