From 87a022cf3560aa4f19c3363e490f9911099744c1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 4 Aug 2011 10:47:47 -0500 Subject: [PATCH] Cache the URI, and added Request::segment method. --- system/request.php | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/system/request.php b/system/request.php index c7a59d9a..a4620417 100644 --- a/system/request.php +++ b/system/request.php @@ -9,6 +9,20 @@ class Request { */ public static $route; + /** + * The request URI. + * + * @var string + */ + public static $uri; + + /** + * The request URI segments. + * + * @var array + */ + public static $segments; + /** * Get the request URI. * @@ -21,6 +35,8 @@ class Request { */ public static function uri() { + if ( ! is_null(static::$uri)) return static::$uri; + if (isset($_SERVER['PATH_INFO'])) { $uri = $_SERVER['PATH_INFO']; @@ -39,19 +55,30 @@ class Request { throw new \Exception("Malformed request URI. Request terminated."); } - // 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)); } - // Remove the application index page from the URI. if (strpos($uri, $index = '/index.php') === 0) { $uri = substr($uri, strlen($index)); } - return (($uri = trim($uri, '/')) == '') ? '/' : $uri; + return static::$uri = (($uri = trim($uri, '/')) == '') ? '/' : $uri; + } + + /** + * Get a segment of the request URI. + * + * @param int $segment + * @return string + */ + public static function segment($segment) + { + if (is_null(static::$segments)) static::$segments = explode('/', static::uri()); + + return (isset(static::$segments[$index = $segment - 1])) ? static::$segments[$index] : null; } /**