Browse Source

add more portable uri support.

Taylor Otwell 13 years ago
parent
commit
a216af62dc
1 changed files with 120 additions and 22 deletions
  1. 120 22
      laravel/uri.php

+ 120 - 22
laravel/uri.php

@@ -16,6 +16,13 @@ class URI {
 	 */
 	public static $segments = array();
 
+	/**
+	 * The server variables to check for the URI.
+	 *
+	 * @var array
+	 */
+	protected static $attempt = array('PATH_INFO', 'REQUEST_URI', 'PHP_SELF', 'REDIRECT_URL');
+
 	/**
 	 * Get the URI for the current request.
 	 *
@@ -25,24 +32,126 @@ class URI {
 	{
 		if ( ! is_null(static::$uri)) return static::$uri;
 
-		$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
+		// To get the URI, we'll first call the detect method which will spin
+		// through each of the server variables that we check for the URI in
+		// and use the first one we encounter for the URI.
+		static::$uri = static::detect();
+
+		// If you ever encounter this error, please information the Laravel
+		// dev team with information about your server. We want to support
+		// Laravel an as many server environments as possible!
+		if (is_null(static::$uri))
+		{
+			throw new \Exception("Could not detect request URI.");
+		}
+
+		static::segments(static::$uri);
+
+		return static::$uri;
+	}
+
+	/**
+	 * Detect the URI from the server variables.
+	 *
+	 * @return string|null
+	 */
+	protected static function detect()
+	{
+		foreach (static::$attempt as $variable)
+		{
+			// Each variable we search for the URI has its own parser function
+			// which is responsible for doing any formatting before the value
+			// is fed into the main formatting function.
+			$method = "parse_{$variable}";
+
+			if (isset($_SERVER[$variable]))
+			{
+				$uri = static::$method($_SERVER[$variable]);
+
+				return static::format($uri);
+			}
+		}		
+	}
+
+	/**
+	 * Format a given URI.
+	 *
+	 * @param  string  $uri
+	 * @return string
+	 */
+	protected static function format($uri)
+	{
+		// First we want to remove the application's base URL from the URI
+		// if it is in the string. It is possible for some of the server
+		// variables to include the entire document root.
+		$uri = static::remove_base($uri);
 
-		// Remove the root application URL from the request URI. If the application
-		// is nested within a sub-directory of the web document root, this will get
-		// rid of all of the the sub-directories from the request URI.
-		$uri = static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
+		$index = '/'.Config::get('application.index');
 
-		// We'll also remove the application's index page as it is not used for at
-		// all for routing and is totally unnecessary as far as the routing of
-		// incoming requests to the framework is concerned.
-		if (($index = '/'.Config::get('application.index')) !== '/')
+		// Next we'll remove the index file from the URI if it is there
+		// and then finally trim down the URI. If the URI is left with
+		// nothing but spaces, we use a single slash for root.
+		if ($index !== '/')
 		{
 			$uri = static::remove($uri, $index);
 		}
 
-		static::segments(static::$uri = static::format($uri));
+		return (($uri = trim($uri, '/')) !== '') ? $uri : '/';
+	}
+
+	/**
+	 * Parse the PATH_INFO server variable.
+	 *
+	 * @param  string  $value
+	 * @return string
+	 */
+	protected static function parse_path_info($value)
+	{
+		return $value;
+	}
 
-		return static::$uri;
+	/**
+	 * Parse the REQUEST_URI server variable.
+	 *
+	 * @param  string  $value
+	 * @return string
+	 */
+	protected static function parse_request_uri($value)
+	{
+		return parse_url($value, PHP_URL_PATH);
+	}
+
+	/**
+	 * Parse the PHP_SELF server variable.
+	 *
+	 * @param  string  $value
+	 * @return string
+	 */
+	protected static function parse_php_self($value)
+	{
+		return $value;
+	}
+
+	/**
+	 * Parse the REDIRECT_URL server variable.
+	 *
+	 * @param  string  $value
+	 * @return string
+	 */
+	protected static function parse_redirect_url($value)
+	{
+		return $value;
+	}
+
+	/**
+	 * Remove the base URL off of the request URI.
+	 *
+	 * @param  string  $uri
+	 * @return string
+	 */
+	protected static function remove_base($uri)
+	{
+		return static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
 	}
 
 	/**
@@ -92,15 +201,4 @@ class URI {
 		return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
 	}
 
-	/**
-	 * Format a given URI.
-	 *
-	 * @param  string  $uri
-	 * @return string
-	 */
-	protected static function format($uri)
-	{
-		return (($uri = trim($uri, '/')) !== '') ? $uri : '/';
-	}
-
 }