MVC中去掉index.php(优雅的使用url)
来源:互联网 发布:淘宝众筹 万创中国 编辑:程序博客网 时间:2024/05/22 09:44
前段时间写了一篇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
- MVC中去掉index.php(优雅的使用url)
- CodeIgniter 的url中去掉index.php
- url中去掉index.php
- 如何去掉CodeIgniter中URL里的index.php
- php 去掉Url里的 index.php
- ThinkPHP-URL中去掉index.php
- 使用URL重写,去掉index.php
- thinkphp 去掉URL 里面的index.php
- thinkphp5 去掉url里的index.php
- 去掉 URL 中的 index.php
- URL如何去掉index.php
- .htaccess文件配置(去掉url里面的index.php)无效url后面加/
- thinkphp在wamp 配置去掉url中index.php方法
- laravel框架如何去掉URL中index.php
- PHP CI(CodeIgniter) 如何去掉url中的index.php
- Apache下简化URL,去掉index.php的方法
- [PHP]去掉CodeIgniter URL中的index.php
- CodeIgniter(CI)去掉默认url中的index.php
- HEX文件和BIN文件区别
- php_pdo详解
- 2016百度之星 hdu 5714 离散化+map+差分数组
- Dubbo 与 Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- 使用JDK自带的方法进行文件解压
- MVC中去掉index.php(优雅的使用url)
- iOS native 与 React Native 个人见解
- Android5.1.1源码 - 在Framework中添加自定义系统服务
- JavaScript基础——引用类型(三)Function类型
- 个人看B站的一些姿势
- maven错误锦集- Could not transfer artifact com.oracle:ojdbc14:jar:10.2.0.4.0
- PPT制作自我介绍的要点
- 简单的九九乘法表
- jdk1.8 和spring 2.5.5 兼容性问题