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

class URI {

	/**
	 * The URI for the current request.
	 *
	 * This property will be set after the URI is detected for the first time.
	 *
	 * @var string
	 */
12
	protected static $uri;
13 14

	/**
15
	 * Determine the request URI.
16
	 *
17 18 19 20 21 22
	 * The request URI will be trimmed to remove to the application URL and application index file.
	 * If the request is to the root of the application, the URI will be set to a forward slash.
	 *
	 * If the $_SERVER "PATH_INFO" variable is available, it will be used; otherwise, we will try
	 * to determine the URI using the REQUEST_URI variable. If neither are available,  an exception
	 * will be thrown by the method.
23 24 25
	 *
	 * @return string
	 */
26
	public static function get()
27
	{
28
		if ( ! is_null(static::$uri)) return static::$uri;
29

30
		if (($uri = static::from_server()) === false)
31 32 33 34
		{
			throw new \Exception('Malformed request URI. Request terminated.');
		}

35
		return static::$uri = static::format(static::clean($uri));
36 37 38 39 40
	}

	/**
	 * Get a given URI segment from the URI for the current request.
	 *
41 42 43 44 45 46 47 48
	 * <code>
	 *		// Get the first URI segment for the request
	 *		$first = URI::segment(1);
	 *
	 *		// Return a default value if the URI segment doesn't exist
	 *		$segment = URI::segment(3, 'Default');
	 * </code>
	 *
49 50 51 52
	 * @param  int     $segment
	 * @param  mixed   $default
	 * @return string
	 */
53
	public static function segment($segment = null, $default = null)
54
	{
55
		$segments = Arr::without(explode('/', static::get()), array(''));
56 57 58 59 60 61 62 63 64 65 66

		if ( ! is_null($segment)) $segment = $segment - 1;

		return Arr::get($segments, $segment, $default);
	}

	/**
	 * Get the request URI from the $_SERVER array.
	 *
	 * @return string
	 */
67
	protected static function from_server()
68 69 70
	{
		// If the PATH_INFO $_SERVER element is set, we will use since it contains
		// the request URI formatted perfectly for Laravel's routing engine.
71
		if (isset($_SERVER['PATH_INFO']))
72
		{
73
			return $_SERVER['PATH_INFO'];
74 75 76 77
		}

		// If the REQUEST_URI is set, we need to extract the URL path since this
		// should return the URI formatted in a manner similar to PATH_INFO.
78
		elseif (isset($_SERVER['REQUEST_URI']))
79
		{
80
			return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
81 82 83 84 85 86 87 88 89 90 91 92 93 94
		}

		throw new \Exception('Unable to determine the request URI.');
	}

	/**
	 * Remove extraneous segments from the URI such as the URL and index page.
	 *
	 * These segments need to be removed since they will cause problems in the
	 * routing engine if they are present in the URI.
	 *
	 * @param  string  $uri
	 * @return string
	 */
95
	protected static function clean($uri)
96
	{
97
		foreach (array(parse_url(Config::get('application.url'), PHP_URL_PATH), '/index.php') as $value)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
		{
			$uri = (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
		}

		return $uri;
	}

	/**
	 * Format the URI.
	 *
	 * If the request URI is empty, a single forward slash will be returned.
	 *
	 * @param  string  $uri
	 * @return string
	 */
113
	protected static function format($uri)
114 115 116 117 118
	{
		return (($uri = trim($uri, '/')) == '') ? '/' : $uri;
	}

}