MY_Router.php 放到 system/application/libraries 目录下,就可以让 CI 的控制器支持多级子目录了。
这样,你就可以在 system/application/controllers 目录下放置更多级别的目录,访问的方式就是 index.php/目录1/目录2/目录3/控制器/方法/参数
请注意,你不需要 load,因为这个类是系统自动 load 的。
MY_Router.php 代码
1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 2 /** 3 * 自定义路由类 4 * 5 * 让CI控制器支持多级目录 6 * 7 * @author SOHOCN.NET 8 * @copyright Copyright © 2012 - 2018 www.sohocn.net All rights reserved. 9 * @created 2012-12-13 10 * @updated 2012-12-13 11 * @version 1.0 12 */ 13 14 class MY_Router extends CI_Router 15 { 16 /** 17 * Set the directory name 18 * 19 * @access public 20 * @param string 21 * @return void 22 */ 23 function set_directory($dir) 24 { 25 $this->directory = $dir.'/'; 26 } 27 28 /** 29 * Validates the supplied segments. Attempts to determine the path to 30 * the controller. 31 * 32 * @access private 33 * @param array 34 * @return array 35 */ 36 37 function _validate_request($segments) 38 { 39 if (count($segments) == 0) 40 { 41 return $segments; 42 } 43 44 // Does the requested controller exist in the root folder? 45 if (file_exists(APPPATH.'controllers/'.$segments[0].'.php')) 46 { 47 return $segments; 48 } 49 50 // Is the controller in a sub-folder? 51 if (is_dir(APPPATH.'controllers/'.$segments[0])) 52 { 53 $temp = array('dir' => array(), 'path' => APPPATH.'controllers/'); 54 55 foreach($segments as $k => $v) 56 { 57 $temp['path'] .= $v.'/'; 58 59 if(is_dir($temp['path'])) 60 { 61 $temp['dir'][] = $v; 62 unset($segments[$k]); 63 } 64 } 65 66 $this->set_directory(implode('/', $temp['dir'])); 67 $segments = array_values($segments); 68 unset($temp); 69 70 if (count($segments) > 0) 71 { 72 // Does the requested controller exist in the sub-folder? 73 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php')) 74 { 75 if ( ! empty($this->routes['404_override'])) 76 { 77 $x = explode('/', $this->routes['404_override']); 78 79 $this->set_directory(''); 80 $this->set_class($x[0]); 81 $this->set_method(isset($x[1]) ? $x[1] : 'index'); 82 83 return $x; 84 } 85 else 86 { 87 show_404($this->fetch_directory().$segments[0]); 88 } 89 } 90 } 91 else 92 { 93 // Is the method being specified in the route? 94 if (strpos($this->default_controller, '/') !== FALSE) 95 { 96 $x = explode('/', $this->default_controller); 97 98 $this->set_class($x[0]); 99 $this->set_method($x[1]); 100 } 101 else 102 { 103 $this->set_class($this->default_controller); 104 $this->set_method('index'); 105 } 106 107 // Does the default controller exist in the sub-folder? 108 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) 109 { 110 $this->directory = ''; 111 return array(); 112 } 113 114 } 115 116 return $segments; 117 } 118 119 120 // If we've gotten this far it means that the URI does not correlate to a valid 121 // controller class. We will now see if there is an override 122 if ( ! empty($this->routes['404_override'])) 123 { 124 $x = explode('/', $this->routes['404_override']); 125 126 $this->set_class($x[0]); 127 $this->set_method(isset($x[1]) ? $x[1] : 'index'); 128 129 return $x; 130 } 131 132 133 // Nothing else to do at this point but show a 404 134 show_404($segments[0]); 135 } 136 } 137 // END MY_Router Class