Refactoring the Request class.
This commit is contained in:
parent
af7ba04628
commit
b1342a5928
|
@ -35,46 +35,42 @@ public static function uri()
|
||||||
elseif (isset($_SERVER['REQUEST_URI']))
|
elseif (isset($_SERVER['REQUEST_URI']))
|
||||||
{
|
{
|
||||||
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||||
|
|
||||||
if ($uri === false)
|
|
||||||
{
|
|
||||||
throw new \Exception("Malformed request URI. Request terminated.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new \Exception('Unable to determine the request URI.');
|
throw new \Exception('Unable to determine the request URI.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------
|
if ($uri === false)
|
||||||
// Remove the application URL.
|
|
||||||
// -------------------------------------------------------
|
|
||||||
$base_url = parse_url(Config::get('application.url'), PHP_URL_PATH);
|
|
||||||
|
|
||||||
if (strpos($uri, $base_url) === 0)
|
|
||||||
{
|
{
|
||||||
$uri = (string) substr($uri, strlen($base_url));
|
throw new \Exception("Malformed request URI. Request terminated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------
|
$uri = static::remove_from_uri($uri, parse_url(Config::get('application.url'), PHP_URL_PATH));
|
||||||
// Remove the application index and any extra slashes.
|
$uri = static::remove_from_uri($uri, '/'.Config::get('application.index'));
|
||||||
// -------------------------------------------------------
|
|
||||||
$index = Config::get('application.index');
|
|
||||||
|
|
||||||
if (strpos($uri, '/'.$index) === 0)
|
|
||||||
{
|
|
||||||
$uri = (string) substr($uri, strlen('/'.$index));
|
|
||||||
}
|
|
||||||
|
|
||||||
$uri = trim($uri, '/');
|
$uri = trim($uri, '/');
|
||||||
|
|
||||||
// -------------------------------------------------------
|
|
||||||
// If the requests is to the root of the application, we
|
|
||||||
// always return a single forward slash.
|
|
||||||
// -------------------------------------------------------
|
|
||||||
return ($uri == '') ? '/' : strtolower($uri);
|
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)
|
||||||
|
{
|
||||||
|
if (strpos($uri, $value) === 0)
|
||||||
|
{
|
||||||
|
$uri = (string) substr($uri, strlen($value));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $uri;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the request method.
|
* Get the request method.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue