为PHP CodeIgniter 框架controllers支持多层目录

阅读更多
Index: Router.php
===================================================================
--- Router.php	(版本 11328)
+++ Router.php	(版本 12362)
@@ -274,13 +274,26 @@
 			return $segments;
 		}
 
+		$dir=$segments[0];
+		$dir_level=0;
+
+		while(is_dir(APPPATH.'controllers/'.$dir)){
+			$dir_level++;
+			if(isset($segments[$dir_level])==false){
+				break;
+			}
+			$dir=$dir."/".$segments[$dir_level];
+		}
+		$dir=join("/",array_slice($segments,0,$dir_level));
+
+		
 		// Is the controller in a sub-folder?
-		if (is_dir(APPPATH.'controllers/'.$segments[0]))
+		if ($dir_level>0)
 		{
+			
 			// Set the directory and remove it from the segment array
-			$this->set_directory($segments[0]);
-			$segments = array_slice($segments, 1);
-
+			$this->set_directory($dir);
+			$segments = array_slice($segments, $dir_level);
 			if (count($segments) > 0)
 			{
 				// Does the requested controller exist in the sub-folder?
@@ -465,7 +478,7 @@
 	 */
 	function set_directory($dir)
 	{
-		$this->directory = str_replace(array('/', '.'), '', $dir).'/';
+		$this->directory = str_replace(array('.'), '', $dir).'/';
 	}
 
 	// --------------------------------------------------------------------

你可能感兴趣的:(php)