Jump to content

Recommended Posts

Hello everyone few days ago i was reading about codeigniters routing system and after that i`ve start searching google about other routing systems and stuff like that.

While i was searching i found out something interesting.

 

$cfg['routing']['url']['products/:alphanum']  = 'store/products/:result';
$cfg['routing']['url']['product\/[a-z]'] = 'store/products/$1';
$cfg['routing']['url']['categories/:num/:alphanum-:num']  = 'store/categories/:result/:result/:result';  

 

can please someone explain to me how this routing system is made maybe a small explained example ? thank  you very much

What is there to explain? All we know is that it's an array. You're the person who found it, so you know more about it than us.

yeah.. maybe..

 

i found a file regarding this config

 

 

class Url
{
private $urlfragments, $mod;
public $url, $file;

	public function __construct( )
	{
		$cfg = Config::getConfig('url');
		$this->mod = (($cfg['rewrite']) == false) ? 1 : 0;
		$this->url = $cfg['url'] . ( $cfg['rewrite'] ? '' : 'index.php/' );
		$nUrl = array();
		if( array_key_exists('PATH_INFO', $_SERVER) )
		{
			$nUrl =  explode('/',$_SERVER['PATH_INFO']);
			$nUrl = array_splice( $nUrl, 1);
		}
		$this->urlfragments = $nUrl;

		if( defined("IN_ADMIN") && IN_ADMIN == true )
		{
			$this->file = 'admin.php';
		}else{
			$this->file = 'index.php';
		}
	}

	public function seg( $num , $returnValue = '' )
	{
		return ( isset( $this->urlfragments[ $num + $this->mod ]) && $this->urlfragments[$num + $this->mod] != '') ? 
					$this->urlfragments[ $num + $this->mod ] : 
					$returnValue;
	}

	public function seg_array( $start, $end, $returnValue = '' )
	{
		$array_splice = array_splice( $this->urlfragments, $start + $this->mod, ($end - $start) + $this->mod);
		return (count($array_splice) > 0 ) ? $array_splice : $returnValue;
	}

	public function seg_all()
	{
		return $this->urlfragments;
	}

	public function seg_fetch_assoc()
	{
		$tick = 0;
		$return = array();
		for( $i = 0 + $this->mod; $i <= count($this->urlfragments); ++$i )
		{
			if( isset($this->urlfragments[$i]) && isset($this->urlfragments[$i + 1]))
			{
				$return[ $this->urlfragments[$i] ] = $this->urlfragments[ ++$i ]; 	 	 	 	
			}elseif( isset($this->urlfragments[$i]) )
			{
				$return[ $this->urlfragments[$i] ] = '';				
			}
		}

		return $return;
	}

	public function seg_assoc( $array = array() )
	{
		$return = '';
		foreach( $array as $key => $val )
		{
			$return .= $key . '/' . $val . '/';			
		}

		return $return;												
	}

	public function seg_return_string( $url = null )
	{
		$url = ( is_null($url) ) ? $this->urlfragments : $url;
		return implode( '/', $url );		
	}

	public function sanitize( $string )
	{
		return str_replace('/', '*', $string);
	}

	public function revert( $string )
	{
		return str_replace('*', '/', $string);
	}


}

 

class Route
{
public static $route, $processed, $isRegex, $isPlain;

	private static function __init()
	{
		//Is route already set?
		if( !is_array(self::$route) )
		{
			//It's not so retreieve the variables from the config file
			$routing = Config::getConfig('routing');

			//Is the config file empty?
			if( is_array($routing) )
			{

				//Are there any rules set?
				$routeCount = count($routing['url']);

				//There may be, so set it
				if( $routeCount > 0 )
				{
					self::$route = $routing;
				}
			}
		}

		self::__processRouteVariables();
	}


	private static function __processRouteVariables()
	{
		self::$isPlain = array();
		self::$isRegex = array();

		//Make sure the variables either havent' been previously
		//processed or the route variable is actually filled
		if( !is_array(self::$route) || self::$processed )
		{
			return;
		}

		//populate variables
		$rules = self::$route['rules'];
		$routes = self::$route['url'];

		//serperate out our rules
		$ruleKeys  = array_keys( $rules );
		$ruleValues = array_values( $rules ); 

		//Make a variable to hold our new rules
		$newRoutes = array();

		//Cyecle through all of them
		foreach( $routes as $routeKey => $routeValue )
		{
			//Take a count of of the black slaces
			$tmpKeyCount = count(explode('/', $routeKey));
			$tmpValCount = count(explode('/', $routeValue));

			//Keep the route values
			$tmpKey = $routeKey;
			$tmpVal = $routeValue;

			//now make them regex safe
			$routeKey   = preg_quote($routeKey);
			$routeValue = preg_quote($routeValue);

			//count the black slashes now
			$pstKeyCount = count(explode('\\', $routeKey));
			$pstValCount = count(explode('\\', $routeValue));

			//echo $tmpKey, '<br />', $tmpKeyCount, '<br />', $pstKeyCount, '<br /><br />';
			//the logic here is quite simple, if the back slashes are more than double what
			//there used to be it means more than the foward slashes and other regex on-safe
			//characters have been quotes meaning this string is a regex string so don't touch
			//it
			if( $pstKeyCount == 1 && !strstr(':', $tmpKey) )
			{
				self::$isPlain[ $tmpKey ] = true;
				$routeKey = $tmpKey;
				$routeValue = $tmpVal;
			}elseif( (($tmpKeyCount * 2) < $pstKeyCount) || (($tmpValCount * 2) < $pstValCount) )
			{
				//put it in the is regex array and refill our values with their original
				//values
				//echo $tmpKey;
				self::$isRegex[ $tmpKey ] = true;
				$routeKey   = $tmpKey;
				$routeValue = $tmpVal;

			}else{
				//This string is a to-be parsed wild card string so parse it
				//and construct our regex string
				$routeKey   = str_replace( $ruleKeys, $ruleValues, $routeKey );
				$routeValue = str_replace( $ruleKeys, $ruleValues, $routeValue );

				//Let's go after results
				if( strstr( $routeValue, ':result') )
				{
					//since the parsed wild-card string has already been made
					//regex safe, we need the temporary string tht hasn't been
					//modified
					$results = explode(':result', $tmpVal);
					$resultCount = count($results) - 1;
					$resultString = null;


					//Cycle through our array and create the result string regex
					for($i = 0; $i < $resultCount; ++$i)
					{
						$resultString .= $results[$i] . '$' . ($i + 1);
					}

					$routeValue = $resultString;

				}
			}

			//Fill our new array of rules
			$newRoutes[ $routeKey ] = $routeValue;
		}

		self::$route = $newRoutes;
		//print_r(self::$route);

	}

	public static function setRoute( Array $route )
	{
		//set the route if the url rule isn't empty
		if( count($route['url']) > 0 )
		{
			self::$route = $route;
		}
	}

	public static function DoRoute($controller, $mod)
	{
		self::__init();

		//after we've ran out initiate function is the route still empty?
		if( !is_array(self::$route) )
		{
			//Indeed, return nothing
			return;
		}

		//Make our mod and controller into a url string that complies
		//with our rules
		$urlString = $controller . '/' . $mod;

		//foreach loops are very break friendly so we're going to make use of a
		//while loop which continues until the end of the array or if a result
		//is found and succesfully replace

		$continue = true;
		while( (list($ruleKey, $ruleValue) = each(self::$route)) && $continue )
		{
			if( array_key_exists($ruleKey, self::$isPlain) )
			{
				$tmpString = str_replace($ruleKey, $ruleValue, $urlString);
				if( $tmpString !== $urlString )
				{
					$urlString = $tmpString;
					$continue = false;
				}	
			}else{
				//Make sure we're not messing with a regex string
				if( !array_key_exists($ruleKey, self::$isRegex) )
				{
					$ruleKey = str_replace('/', '\/', $ruleKey);
				}

				//Check if the string is a match
				if( preg_match( '/' . $ruleKey . '/', $urlString) && $continue == true )
				{
					//It is so repalce it and break the loop
					$urlString = preg_replace( '/' . $ruleKey . '/', $ruleValue, $urlString);
					$continue = false;
				}
			}
		}

		//now break the url string again and return the controller, the function, and the variables
		$return = array();
		$urlString = explode('/', $urlString);
		$url = count($urlString)-1;


		//Build an array with our controller, method, and varaibles for input
		$return['controller'] = $urlString[0];
		$return['method']	  = $urlString[1];

		for($i = 2; $i <= $url; ++$i)
		{
			$return['vars'][] = $urlString[$i];
		}

		return $return;
	}
}

 

but i didn't understand exacly what it does how it does.. :(

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.