1: <?php namespace Laravel;
2:
3: class URI {
4:
5: /**
6: * The URI for the current request.
7: *
8: * @var string
9: */
10: public static $uri;
11:
12: /**
13: * The URI segments for the current request.
14: *
15: * @var array
16: */
17: public static $segments = array();
18:
19: /**
20: * Get the full URI including the query string.
21: *
22: * @return string
23: */
24: public static function full()
25: {
26: return Request::getUri();
27: }
28:
29: /**
30: * Get the URI for the current request.
31: *
32: * @return string
33: */
34: public static function current()
35: {
36: if ( ! is_null(static::$uri)) return static::$uri;
37:
38: // We'll simply get the path info from the Symfony Request instance and then
39: // format to meet our needs in the router. If the URI is root, we'll give
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: * Format a given URI.
50: *
51: * @param string $uri
52: * @return string
53: */
54: protected static function format($uri)
55: {
56: return trim($uri, '/') ?: '/';
57: }
58:
59: /**
60: * Determine if the current URI matches a given pattern.
61: *
62: * @param string $pattern
63: * @return bool
64: */
65: public static function is($pattern)
66: {
67: return Str::is($pattern, static::current());
68: }
69:
70: /**
71: * Get a specific segment of the request URI via a one-based index.
72: *
73: * <code>
74: * // Get the first segment of the request URI
75: * $segment = URI::segment(1);
76: *
77: * // Get the second segment of the URI, or return a default value
78: * $segment = URI::segment(2, 'Taylor');
79: * </code>
80: *
81: * @param int $index
82: * @param mixed $default
83: * @return string
84: */
85: public static function segment($index, $default = null)
86: {
87: static::current();
88:
89: return array_get(static::$segments, $index - 1, $default);
90: }
91:
92: /**
93: * Set the URI segments for the request.
94: *
95: * @param string $uri
96: * @return void
97: */
98: protected static function segments($uri)
99: {
100: $segments = explode('/', trim($uri, '/'));
101:
102: static::$segments = array_diff($segments, array(''));
103: }
104:
105: }