2011-11-14 21:18:18 -06:00
|
|
|
<?php namespace Laravel;
|
|
|
|
|
|
|
|
|
|
class URI {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The URI for the current request.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2011-11-15 19:15:31 -06:00
|
|
|
public static $uri;
|
2011-11-14 21:18:18 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The URI segments for the current request.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static $segments = array();
|
2011-11-14 21:18:18 -06:00
|
|
|
|
2012-02-12 14:48:36 -06:00
|
|
|
/**
|
|
|
|
|
* Get the full URI including the query string.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function full()
|
|
|
|
|
{
|
2012-03-28 16:15:21 -05:00
|
|
|
return Request::getUri();
|
2012-02-12 14:48:36 -06:00
|
|
|
}
|
2012-02-08 14:27:22 -06:00
|
|
|
|
2011-11-14 21:18:18 -06:00
|
|
|
/**
|
|
|
|
|
* Get the URI for the current request.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function current()
|
|
|
|
|
{
|
|
|
|
|
if ( ! is_null(static::$uri)) return static::$uri;
|
|
|
|
|
|
2012-03-28 16:15:21 -05:00
|
|
|
// We'll simply get the path info from the Symfony Request instance and then
|
|
|
|
|
// format to meet our needs in the router. If the URI is root, we'll give
|
2012-05-03 08:28:40 -05:00
|
|
|
// back a single slash, otherwise we'll strip all of the slashes off.
|
2012-03-28 16:15:21 -05:00
|
|
|
$uri = static::format(Request::getPathInfo());
|
2012-02-08 14:27:22 -06:00
|
|
|
|
2012-03-28 16:15:21 -05:00
|
|
|
static::segments($uri);
|
2012-02-08 14:27:22 -06:00
|
|
|
|
2012-03-28 16:15:21 -05:00
|
|
|
return static::$uri = $uri;
|
2012-02-08 14:27:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format a given URI.
|
|
|
|
|
*
|
|
|
|
|
* @param string $uri
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function format($uri)
|
|
|
|
|
{
|
2012-02-08 14:35:30 -06:00
|
|
|
return trim($uri, '/') ?: '/';
|
2012-02-08 14:27:22 -06:00
|
|
|
}
|
|
|
|
|
|
2012-02-12 14:48:36 -06:00
|
|
|
/**
|
|
|
|
|
* Determine if the current URI matches a given pattern.
|
|
|
|
|
*
|
|
|
|
|
* @param string $pattern
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2012-04-12 22:54:50 -05:00
|
|
|
public static function is($pattern)
|
2012-02-12 14:48:36 -06:00
|
|
|
{
|
2012-04-12 22:54:50 -05:00
|
|
|
return Str::is($pattern, static::current());
|
2012-02-12 14:48:36 -06:00
|
|
|
}
|
|
|
|
|
|
2011-11-14 21:18:18 -06:00
|
|
|
/**
|
|
|
|
|
* Get a specific segment of the request URI via an one-based index.
|
|
|
|
|
*
|
|
|
|
|
* <code>
|
|
|
|
|
* // Get the first segment of the request URI
|
|
|
|
|
* $segment = URI::segment(1);
|
|
|
|
|
*
|
|
|
|
|
* // Get the second segment of the URI, or return a default value
|
|
|
|
|
* $segment = URI::segment(2, 'Taylor');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
|
|
|
|
* @param int $index
|
|
|
|
|
* @param mixed $default
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function segment($index, $default = null)
|
|
|
|
|
{
|
|
|
|
|
static::current();
|
|
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
return array_get(static::$segments, $index - 1, $default);
|
2011-11-14 21:18:18 -06:00
|
|
|
}
|
|
|
|
|
|
2012-02-04 20:13:10 -06:00
|
|
|
/**
|
|
|
|
|
* Set the URI segments for the request.
|
|
|
|
|
*
|
|
|
|
|
* @param string $uri
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2012-02-04 20:13:30 -06:00
|
|
|
protected static function segments($uri)
|
2012-02-04 20:13:10 -06:00
|
|
|
{
|
|
|
|
|
$segments = explode('/', trim($uri, '/'));
|
|
|
|
|
|
|
|
|
|
static::$segments = array_diff($segments, array(''));
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 21:18:18 -06:00
|
|
|
}
|