Return to Snippet

Revision: 43804
at March 31, 2011 03:58 by Josh


Initial Code
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Router Class
 *
 * Parses URIs and determines routing
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @author		ExpressionEngine Dev Team
 * @category	Libraries
 * @link		http://codeigniter.com/user_guide/general/routing.html
 */
class MY_Router extends CI_Router {

	function __construct()
	{
		parent::__construct();
	}

	/**
	 *  Parse Routes
	 *
	 * This function matches any routes that may exist in
	 * the config/routes.php file against the URI to
	 * determine if the class/method need to be remapped.
	 *
	 * @access	private
	 * @return	void
	 */
	function _parse_routes()
	{
		// Turn the segment array into a URI string
		$uri = implode('/', $this->uri->segments);

		// Is there a literal match?  If so we're done
		if (isset($this->routes[$uri]))
		{
			return $this->_set_request(explode('/', $this->routes[$uri]));
		}

		// Loop through the route array looking for wild-cards
		foreach ($this->routes as $key => $val)
		{
			/* 
			// OLD CODEIGNITER WILD-CARD REGEX
			
			// Convert wild-cards to RegEx
			$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
			
			*/
			
			// Convert wild-cards to RegEx
			// Additionally added :alpha to the wild cards.
			// @Author Josh Manders
			// @Website joshmanders.com
			$wild_cards = array(':any' => '.+', ':num' => '[0-9]+', ':alpha' => '[a-zA-Z]+');
			foreach ($wild_cards as $wild_card => $regex)
			{
				$key = str_replace($wild_card, $regex, $key);
			}

			// Does the RegEx match?
			if (preg_match('#^'.$key.'$#', $uri))
			{
				// Do we have a back-reference?
				if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
				{
					$val = preg_replace('#^'.$key.'$#', $val, $uri);
				}

				return $this->_set_request(explode('/', $val));
			}
		}

		// If we got this far it means we didn't encounter a
		// matching route so we'll set the site default route
		$this->_set_request($this->uri->segments);
	}
}
// END Router Class

/* End of file MY_Router.php */
/* Location: ./app/core/MY_Router.php */

Initial URL

                                

Initial Description
Just save this as MY_Router.php and upload to application/core/ and now you'll be able to use us :alpha along with :num and :any in your routes to signify that you just want to match a-z.

Initial Title
Allow :alpha in routes for CodeIgniter 2.0.1 Reactor.

Initial Tags
codeigniter

Initial Language
PHP