uri.php 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php namespace Laravel;

class URI {

	/**
	 * The URI for the current request.
	 *
	 * @var string
	 */
Taylor Otwell committed
10
	public static $uri;
11 12 13 14 15 16

	/**
	 * The URI segments for the current request.
	 *
	 * @var array
	 */
17
	public static $segments = array();
18 19

	/**
20 21 22 23 24 25
	 * Get the full URI including the query string.
	 *
	 * @return string
	 */
	public static function full()
	{
26
		return Request::getUri();
27
	}
28 29

	/**
30 31 32 33 34 35 36 37
	 * Get the URI for the current request.
	 *
	 * @return string
	 */
	public static function current()
	{
		if ( ! is_null(static::$uri)) return static::$uri;

38 39
		// 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
Taylor Otwell committed
40
		// back a single slash, otherwise we'll strip all of the slashes off.
41
		$uri = static::format(Request::getPathInfo());
42

43
		static::segments($uri);
44

45
		return static::$uri = $uri;
46 47 48 49 50 51 52 53 54 55
	}

	/**
	 * Format a given URI.
	 *
	 * @param  string  $uri
	 * @return string
	 */
	protected static function format($uri)
	{
56
		return trim($uri, '/') ?: '/';
57 58 59
	}

	/**
60 61 62 63 64
	 * Determine if the current URI matches a given pattern.
	 *
	 * @param  string  $pattern
	 * @return bool
	 */
65
	public static function is($pattern)
66
	{
67
		return Str::is($pattern, static::current());
68 69 70
	}

	/**
Chris Berthe committed
71
	 * Get a specific segment of the request URI via a one-based index.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	 *
	 * <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();

89
		return array_get(static::$segments, $index - 1, $default);
90 91 92
	}

	/**
93 94 95 96 97
	 * Set the URI segments for the request.
	 *
	 * @param  string  $uri
	 * @return void
	 */
Taylor Otwell committed
98
	protected static function segments($uri)
99 100 101 102 103 104
	{
		$segments = explode('/', trim($uri, '/'));

		static::$segments = array_diff($segments, array(''));
	}

105
}