From 141bed39162431c0768c60b445d9414f68a5fe75 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 9 Jul 2011 22:31:06 -0500 Subject: [PATCH] cleaning up Request::uri method. --- system/request.php | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/system/request.php b/system/request.php index a65a96d1..6381dd43 100644 --- a/system/request.php +++ b/system/request.php @@ -2,13 +2,6 @@ class Request { - /** - * The request URI. - * - * @var string - */ - public static $uri; - /** * The route handling the current request. * @@ -19,15 +12,15 @@ class Request { /** * Get the request URI. * + * The server PATH_INFO will be used if available. Otherwise, the REQUEST_URI will be used. + * The application URL and index will be removed from the URI. + * + * If the request is to the root of application, a single forward slash will be returned. + * * @return string */ public static function uri() { - if ( ! is_null(static::$uri)) - { - return static::$uri; - } - if (isset($_SERVER['PATH_INFO'])) { $uri = $_SERVER['PATH_INFO']; @@ -46,24 +39,19 @@ public static function uri() throw new \Exception("Malformed request URI. Request terminated."); } - $uri = static::remove_from_uri($uri, parse_url(Config::get('application.url'), PHP_URL_PATH)); - $uri = static::remove_from_uri($uri, '/'.Config::get('application.index')); + // Remove the application URL from the URI. + if (strpos($uri, $base = parse_url(Config::get('application.url'), PHP_URL_PATH)) === 0) + { + $uri = substr($uri, strlen($base)); + } - $uri = trim($uri, '/'); + // Remove the application index page from the URI. + if (strpos($uri, $index = '/'.Config::get('application.index')) === 0) + { + $uri = substr($uri, strlen($index)); + } - return ($uri == '') ? '/' : strtolower($uri); - } - - /** - * Remove a string from the beginning of a URI. - * - * @param string $uri - * @param string $value - * @return string - */ - private static function remove_from_uri($uri, $value) - { - return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri; + return (($uri = trim($uri, '/')) == '') ? '/' : strtolower($uri); } /** @@ -156,6 +144,7 @@ public static function route_is($name) */ public static function __callStatic($method, $parameters) { + // Dynamically determine if a given route is handling the request. if (strpos($method, 'route_is_') === 0) { return static::route_is(substr($method, 9));