Commits

admin  committed 25de727

  • Participants
  • Parent commits 58b142f

Comments (0)

Files changed (3)

File system/codeigniter/CodeIgniter.php

 
 /*
  * ------------------------------------------------------
- *  Does the requested controller exist?
- * ------------------------------------------------------
- */
-if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_class().EXT))
-{
-	show_404();
-}
-
-/*
- * ------------------------------------------------------
  *  Load the remaining base classes
  * ------------------------------------------------------
  */
  * 
  */
  
- 
 _load_class('CI_Loader'); 
   
 if (floor(phpversion()) < 5)
 
 _load_class('CI_Controller'); 
 
-require(APPPATH.'controllers/'.$RTR->fetch_class().EXT);
+require(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT);
 
 /*
  * ------------------------------------------------------

File system/libraries/Router.php

 	var $routes 		= array();
 	var $class			= '';
 	var $method			= 'index';
+	var $directory		= '';
 	var $uri_protocol 	= 'auto';
 	var $default_controller;
 	var $scaffolding_request = FALSE; // Must be set to FALSE
 			log_message('debug', "No URI present. Default controller set.");
 			return;
 		}
+		unset($this->routes['default_controller']);
 		
 		// Do we need to remove the suffix specified in the config file?
 		if  ($this->config->item('url_suffix') != "")
 		}
 
 		// Explode the URI Segments. The individual segments will
-		// be stored in the $this->segments array.
-		$this->_compile_segments(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)));
-
-
-		// Remap the class/method if a route exists
-		unset($this->routes['default_controller']);
+		// be stored in the $this->segments array.	
+		$i = 1;
+		foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val)
+		{
+			// Filter segments for security
+			$val = trim($this->_filter_uri($val));
+			
+			if ($val != '')
+				$this->segments[$i++] = $val;
+		}
 		
+		$this->_compile_segments($this->segments);
+		
+		// Do we have any custom routing to deal with?
 		if (count($this->routes) > 0)
 		{
 			$this->_parse_routes();
 	 * @param	bool
 	 * @return	void
 	 */
-	function _compile_segments($segs, $route = FALSE)
-	{		
-		$segments = array();
-	
-		$i = 1;
-		foreach($segs as $val)
+	function _compile_segments($segments = array())
+	{	
+		$segments = $this->_validate_segments($segments);
+		
+		if (count($segments) == 0)
 		{
-			$val = trim($this->_filter_uri($val));
-			
-			if ($val != '')
-				$segments[$i++] = $val;
+			return;
 		}
-		
+						
 		$this->set_class($segments['1']);
 		
 		if (isset($segments['2']))
 				$this->set_method($segments['2']);
 			}
 		}
-		
-		if ($route == FALSE)
+	}
+	// END _compile_segments()
+	
+	// --------------------------------------------------------------------
+	
+	/**
+	 * Validates the supplied segments.  Attempts to determine the path to
+	 * the controller.
+	 *
+	 * @access	private
+	 * @param	array
+	 * @return	array
+	 */	
+	function _validate_segments($segments)
+	{
+		// Does the requested controller exist?
+		if ( ! file_exists(APPPATH.'controllers/'.$segments['1'].EXT))
 		{
-			$this->segments = $segments;
+			// Is it a directory?  No?  Smite them!
+			if ( ! is_dir(APPPATH.'controllers/'.$segments['1']))
+			{
+				show_404();
+			}
+			else
+			{
+				$this->set_directory($segments['1']);	
+				$segs = array_slice($segments, 1);
+				
+				if (count($segs) == 0)
+				{
+					$this->set_class($this->default_controller);
+					$this->set_method('index');
+					$this->directory = '';
+					return array();
+				}
+	
+				$i = 1;
+				$segments = array();
+				foreach ($segs as $val)
+				{
+					$segments[$i++] = $val;
+				}
+			}
 		}
 		
-		unset($segments);
+		return $segments;
 	}
-	// END _compile_segments()
+	// END _validate_segments()
 	
 	// --------------------------------------------------------------------
 	
 	// --------------------------------------------------------------------
 	
 	/**
-	 * Set the class name
-	 *
-	 * @access	public
-	 * @param	string
-	 * @return	void
-	 */	
-	function set_class($class)
-	{
-		$this->class = $class;
-	}
-	// END _filter_uri()
-	
-	// --------------------------------------------------------------------
-	
-	/**
-	 * Fetch the current class
-	 *
-	 * @access	public
-	 * @return	string
-	 */	
-	function fetch_class()
-	{
-		return $this->class;
-	}
-	// END _filter_uri()
-	
-	// --------------------------------------------------------------------
-	
-	/**
-	 *  Set the method name
-	 *
-	 * @access	public
-	 * @param	string
-	 * @return	void
-	 */	
-	function set_method($method)
-	{
-		$this->method = $method;
-	}
-	// END set_method()
-	
-	// --------------------------------------------------------------------
-	
-	/**
-	 *  Fetch the current method
-	 *
-	 * @access	public
-	 * @return	string
-	 */	
-	function fetch_method()
-	{
-		return $this->method;
-	}
-	// END set_method()
-	
-	// --------------------------------------------------------------------
-	
-	/**
 	 *  Parse Routes
 	 *
 	 * This function matches any routes that may exist in
 		// Is there a literal match?  If so we're done
 		if (isset($this->routes[$uri]))
 		{
-			$this->_compile_segments(explode('/', $this->routes[$uri]), TRUE);		
+			$this->_compile_segments(explode('/', $this->routes[$uri]));		
 			return;
 		}
 		
 			// Convert wildcards to RegEx
 			$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
 			
-			// Does the regex match this URI ?
+			// Does the RegEx match?
 			if (preg_match('|^'.$key.'$|', $uri))
 			{			
-				// Do we have a replacemnt?
+				// Do we have a back-reference?
 				if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
 				{
 					$val = preg_replace('|^'.$key.'$|', $val, $uri);
 				}
 			
-				$this->_compile_segments(explode('/', $val), TRUE);		
-				break;
+				$this->_compile_segments(explode('/', $val));		
+				return;
 			}
-		
 		}	
 	}
 	// END set_method()
+
+	// --------------------------------------------------------------------
+	
+	/**
+	 * Set the class name
+	 *
+	 * @access	public
+	 * @param	string
+	 * @return	void
+	 */	
+	function set_class($class)
+	{
+		$this->class = $class;
+	}
+	// END set_class()
+	
+	// --------------------------------------------------------------------
+	
+	/**
+	 * Fetch the current class
+	 *
+	 * @access	public
+	 * @return	string
+	 */	
+	function fetch_class()
+	{
+		return $this->class;
+	}
+	// END fetch_class()
+	
+	// --------------------------------------------------------------------
+	
+	/**
+	 *  Set the method name
+	 *
+	 * @access	public
+	 * @param	string
+	 * @return	void
+	 */	
+	function set_method($method)
+	{
+		$this->method = $method;
+	}
+	// END set_method()
+
+	// --------------------------------------------------------------------
+	
+	/**
+	 *  Fetch the current method
+	 *
+	 * @access	public
+	 * @return	string
+	 */	
+	function fetch_method()
+	{
+		return $this->method;
+	}
+	// END fetch_method()
+
+	// --------------------------------------------------------------------
+	
+	/**
+	 *  Set the directory name
+	 *
+	 * @access	public
+	 * @param	string
+	 * @return	void
+	 */	
+	function set_directory($dir)
+	{
+		$this->directory = $dir.'/';
+	}
+	// END set_directory()
+
+	// --------------------------------------------------------------------
+	
+	/**
+	 *  Fetch the sub-directory (if any) that contains the requested controller class
+	 *
+	 * @access	public
+	 * @return	string
+	 */	
+	function fetch_directory()
+	{
+		return $this->directory;
+	}
+	// END fetch_directory()
+
 }
 // END Router Class
 ?>

File user_guide/general/changelog.html

 <li>Added <a href="hooks.html">Hooks</a> feature, enabling you to tap into and modify the inner workings of the framework without hacking the core files.</li>
 <li>Added the ability to <a href="core_classes.html">replace core system classes</a> with your own classes.</li>
 <li>Added support for % character in URL.</li>
+<li>Added the ability to organize <a href="controllers.html">controller</a> files into sub-folders.
 <li>Updated the <a href="routing.html">Routing feature</a> to accept regular expressions within routing rules.</li>
 <li>Moved the MIME type array out of the Upload class and into its own file in the applications/comfig/ folder.</li>
 <li>Removed a strtolower() call that was changing URL segments to lower case.</li>