Cache the URI, and added Request::segment method.
This commit is contained in:
parent
ede3e12620
commit
87a022cf35
|
@ -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 {
|
||||||
*/
|
*/
|
||||||
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 @@ public static function uri()
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue