Commit 87a022cf authored by Taylor Otwell's avatar Taylor Otwell

Cache the URI, and added Request::segment method.

parent ede3e126
...@@ -9,6 +9,20 @@ class Request { ...@@ -9,6 +9,20 @@ class Request {
*/ */
public static $route; public static $route;
/**
* The request URI.
*
* @var string
*/
public static $uri;
/**
* The request URI segments.
*
* @var array
*/
public static $segments;
/** /**
* Get the request URI. * Get the request URI.
* *
...@@ -21,6 +35,8 @@ class Request { ...@@ -21,6 +35,8 @@ class Request {
*/ */
public static function uri() public static function uri()
{ {
if ( ! is_null(static::$uri)) return static::$uri;
if (isset($_SERVER['PATH_INFO'])) if (isset($_SERVER['PATH_INFO']))
{ {
$uri = $_SERVER['PATH_INFO']; $uri = $_SERVER['PATH_INFO'];
...@@ -39,19 +55,30 @@ class Request { ...@@ -39,19 +55,30 @@ class Request {
throw new \Exception("Malformed request URI. Request terminated."); 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) if (strpos($uri, $base = parse_url(Config::get('application.url'), PHP_URL_PATH)) === 0)
{ {
$uri = substr($uri, strlen($base)); $uri = substr($uri, strlen($base));
} }
// Remove the application index page from the URI.
if (strpos($uri, $index = '/index.php') === 0) if (strpos($uri, $index = '/index.php') === 0)
{ {
$uri = substr($uri, strlen($index)); $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;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment