我们在使用项目中 比如说 tp 或 ci 的路由规则都是 pathinfo的格式,
现在我们也在写项目,那么pathinfo的格式是如何实现的?
看看我们的代码吧
<?php
/**
* Created by PhpStorm.
* User: DELL
* Date: 2018/1/6
* Time: 17:46
*/
namespace core;
class Route{
public $model;
public $controller;
public $action;
public function __construct(){
if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']!="/" && isset($_SERVER['PATH_INFO'])){
$path=$_SERVER['PATH_INFO'];
$path_arr=explode("/",trim($path,'/'));
if(isset($path_arr[0])){
$this->controller=$path_arr[0]."Controller";
unset($path_arr[0]);
}else{
$this->controller=config("default_controller")."Controller";
}
if(isset($path_arr[1])){
$this->action=str_replace(config("prefix"),"",$path_arr[1]);
unset($path_arr[1]);
}else{
$this->action=config("default_action");
}
//剩下的参数放在get中 为什么加2 因为前边已经去掉了两个 为了照顾下标所以加2
$argsSize=sizeof($path_arr)+2;
//如果是奇数就去掉最后一个,并且数量减去1
if($argsSize%2==1){array_pop($path_arr);$argsSize--;}
for($i=2;$i<$argsSize;$i=$i+2){
$_GET[$path_arr[$i]]=$path_arr[$i+1];
}
}else{
$this->controller=config("default_controller")."Controller";
$this->action=config("default_action");
}
}
}