url.php 5.24 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2 3 4 5

class URL {

	/**
6 7 8 9 10 11
	 * Get the base URL of the application.
	 *
	 * @return string
	 */
	public static function base()
	{
12
		if (($base = Config::get('application.url')) !== '') return $base;
13 14 15 16 17

		if (isset($_SERVER['HTTP_HOST']))
		{
			$protocol = (Request::secure()) ? 'https://' : 'http://';

18 19 20 21
			// Basically, by removing the basename, we are removing everything after the
			// and including the front controller from the request URI. Leaving us with
			// the path in which the framework is installed. From that path, we can
			// construct the base URL to the application.
22 23 24 25 26 27 28 29 30
			$path = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

			return rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/');
		}

		return 'http://localhost';
	}

	/**
31 32
	 * Generate an application URL.
	 *
33 34 35
	 * <code>
	 *		// Create a URL to a location within the application
	 *		$url = URL::to('user/profile');
Taylor Otwell committed
36 37 38
	 *
	 *		// Create a HTTPS URL to a location within the application
	 *		$url = URL::to('user/profile', true);
39 40
	 * </code>
	 *
41
	 * @param  string  $url
42
	 * @param  bool    $https
43 44
	 * @return string
	 */
45
	public static function to($url = '', $https = false)
46
	{
Taylor Otwell committed
47
		if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
48

49
		$root = static::base().'/'.Config::get('application.index');
50

51
		// Since SSL is not often used while developing the application, we allow the
52
		// developer to disable SSL on all framework generated links to make it more
53 54
		// convenient to work with the site while developing locally.
		if ($https and Config::get('application.ssl'))
55
		{
Taylor Otwell committed
56
			$root = preg_replace('~http://~', 'https://', $root, 1);
57
		}
58

Taylor Otwell committed
59
		return rtrim($root, '/').'/'.ltrim($url, '/');
60 61 62 63 64 65 66 67
	}

	/**
	 * Generate an application URL with HTTPS.
	 *
	 * @param  string  $url
	 * @return string
	 */
68
	public static function to_secure($url = '')
69
	{
70
		return static::to($url, true);
71 72 73
	}

	/**
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	 * Generate a URL to a controller action.
	 *
	 * <code>
	 *		// Generate a URL to the "index" method of the "user" controller
	 *		$url = URL::to_action('user@index');
	 *
	 *		// Generate a URL to http://example.com/user/profile/taylor
	 *		$url = URL::to_action('user@profile', array('taylor'));
	 * </code>
	 *
	 * @param  string  $action
	 * @param  array   $parameters
	 * @param  bool    $https
	 * @return string
	 */
	public static function to_action($action, $parameters = array(), $https = false)
	{
91 92 93
		$action = str_replace(array('.', '@'), '/', $action);

		return static::to($action.'/'.implode('/', $parameters), $https);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	}

	/**
	 * Generate a HTTPS URL to a controller action.
	 *
	 * @param  string  $action
	 * @param  array   $parameters
	 * @return string
	 */
	public static function to_secure_action($action, $parameters = array())
	{
		return static::to_action($action, $parameters, true);
	}

	/**
109
	 * Generate an application URL to an asset.
110
	 *
111 112
	 * @param  string  $url
	 * @param  bool    $https
113 114
	 * @return string
	 */
115
	public static function to_asset($url, $https = null)
116
	{
117
		if (is_null($https)) $https = Request::secure();
118

119
		$url = static::to($url, $https);
120

121 122 123 124 125 126 127
		// Since assets are not served by Laravel, we do not need to come through
		// the front controller. So, we'll remove the application index specified
		// in the application configuration from the generated URL.
		if (($index = Config::get('application.index')) !== '')
		{
			$url = str_replace($index.'/', '', $url);
		}
128

129
		return $url;
130 131 132
	}

	/**
133
	 * Generate a URL from a route name.
134 135 136
	 *
	 * <code>
	 *		// Create a URL to the "profile" named route
137
	 *		$url = URL::to_route('profile');
138
	 *
139 140
	 *		// Create a URL to the "profile" named route with wildcard parameters
	 *		$url = URL::to_route('profile', array($username));
141
	 * </code>
142 143 144 145 146
	 *
	 * @param  string  $name
	 * @param  array   $parameters
	 * @param  bool    $https
	 * @return string
147
	 */
148
	public static function to_route($name, $parameters = array(), $https = false)
149
	{
150
		if (is_null($route = Routing\Router::find($name)))
151
		{
152
			throw new \Exception("Error creating URL for undefined route [$name].");
153 154
		}

155 156 157 158 159 160 161 162 163 164 165 166
		$uris = explode(', ', key($route));

		// Routes can handle more than one URI, but we will just take the first URI
		// and use it for the URL. Since all of the URLs should point to the same
		// route, it doesn't make a difference.
		$uri = substr($uris[0], strpos($uris[0], '/'));

		// Spin through each route parameter and replace the route wildcard segment
		// with the corresponding parameter passed to the method. Afterwards, we'll
		// replace all of the remaining optional URI segments with spaces since
		// they may not have been specified in the array of parameters.
		foreach ((array) $parameters as $parameter)
167
		{
168
			$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
169 170
		}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		// If there are any remaining optional place-holders, we'll just replace
		// them with empty strings since not every optional parameter has to be
		// in the array of parameters that were passed into the method.
		$uri = str_replace(array('/(:any?)', '/(:num?)'), '', $uri);

		return static::to($uri, $https);
	}

	/**
	 * Generate a HTTPS URL from a route name.
	 *
	 * @param  string  $name
	 * @param  array   $parameters
	 * @return string
	 */
	public static function to_secure_route($name, $parameters = array())
	{
		return static::to_route($name, $parameters, true);
189 190
	}

191
}