关闭

MVC中去掉index.php(优雅的使用url)

513人阅读 评论(0) 收藏 举报
分类:

前段时间写了一篇MVC中的简单路由
后来发现,该文章实现的功能,支持controller里面的路径,已经url参数的设置,因此,今天把优化的代码贴上。
config.php

<?php
/**
 * Created by PhpStorm.
 * User: koastal
 * Date: 2016/5/15
 * Time: 10:48
 */
define("APP_PATH",substr(__DIR__,0,strlen(__DIR__)-5));
define("Controller_PATH",APP_PATH.DIRECTORY_SEPARATOR."controller");
define("Model_PATH",APP_PATH.DIRECTORY_SEPARATOR."model");
define("View_PATH",APP_PATH.DIRECTORY_SEPARATOR."view");
define("Lib_PATH",APP_PATH.DIRECTORY_SEPARATOR."libs");

Loader.php

<?php

/**
 * Created by PhpStorm.
 * User: koastal
 * Date: 2016/5/15
 * Time: 12:03
 */
require_once 'config.php';
class Loader
{
    public  static function baseLoad()
    {
        require_once("Controller.php");
        require_once("Model.php");
    }
    public static function searchFile($filename,$path)
    {
        $filePath = false;
        $list = scandir($path);
        foreach($list as $file){
            $realPath = $path.DIRECTORY_SEPARATOR.$file;
            if(is_dir($realPath) && $file!='.' && $file!='..'){
                $res = Loader::searchFile($filename,$realPath);
                if($res){
                    return $res;
                }
            }elseif($file!='.' && $file!='..'){
                if($file == $filename){
                    $filePath = $realPath;
                    break;
                }
            }
        }
        return $filePath;
    }
    public static function autoload($class)
    {
        $filename = $class.".class.php";
        $cflag = Loader::searchFile($filename,Controller_PATH);
        $mfalg = Loader::searchFile($filename,Model_PATH);
        $path = false;
        $path = ($cflag != false)? $cflag:$path;
        $path = ($mfalg != false)? $mfalg:$path;
        if($path == false){
            exit("Class Load Failed.");
        }else{
            require_once($path);
        }
    }
}
Loader::baseLoad();
spl_autoload_register(array('Loader', 'autoload'));

router.php

<?php
/**
 * Created by PhpStorm.
 * User: koastal
 * Date: 2016/5/15
 * Time: 15:09
 */
require_once("config.php");
require_once("Loader.php");
$path = isset($_SERVER['PATH_INFO'])? $_SERVER['PATH_INFO']:null;
$path = str_replace("index.php/",'',$path);
$path_array = explode("/",$path);
array_shift($path_array);   //得到完整参数
if(count($path_array)<3){
    switch(count($path_array)){
        case 2:
            $controller = $path_array[0];
            $function = $path_array[1];
            break;
        case 1:
            $controller = $path_array[0];
            $function = "index";
            break;
        case 0:
            $controller = "index";
            $function = "index";
            break;
    }
}else{
    $param = array();   //参数
    foreach($path_array as $num=>$name){
        $temp_dir = Controller_PATH."/".$name;
        if(is_dir($temp_dir)){
            array_shift($path_array);
            continue;
        }else{      //控制器
            $controller = isset($path_array[$num])? $path_array[$num]:'index';
            $function = isset($path_array[($num+1)])? $path_array[($num+1)]:'index';
            array_shift($path_array);array_shift($path_array);
            $param = $path_array;
            break;
        }
    }
    if(!empty($param)){ //处理参数
        for($i=0;$i<count($param);$i=$i+2){
            $key = $param[$i];
            $value = isset($param[($i+1)])? $param[($i+1)]:null;
            $_GET[$key] = $value;
        }
    }
}

$controller .= "Controller";
$controller_obj = new $controller;
$controller_obj->$function();
?>

与上一篇文章的优化之处就是增强了对$path_array的判断,首先判断前面的参数是否为路径,如果为路径则从数组中弹出,直到取到控制器和方法,然后将控制器和方法也从数组中弹出,剩下的就是url传递给方法的参数了。

全文完。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:95075次
    • 积分:2601
    • 等级:
    • 排名:第14049名
    • 原创:170篇
    • 转载:5篇
    • 译文:0篇
    • 评论:16条
    最新评论