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

class URL {

	/**
	 * Generate an application URL.
	 *
Taylor Otwell committed
8 9
	 * If the given URL is already well-formed, it will be returned unchanged.
	 *
10 11 12 13 14 15 16 17
	 * <code>
	 *		// Generate an application URL from a given URI
	 *		echo URL::to('user/profile');
	 *
	 *		// Generate an application URL with HTTPS
	 *		echo URL::to('user/profile', true);
	 * </code>
	 *
18
	 * @param  string  $url
19
	 * @param  bool    $https
20 21
	 * @return string
	 */
22
	public static function to($url = '', $https = false)
23
	{
Taylor Otwell committed
24
		if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
25

26
		$base = Config::get('application.url').'/'.Config::get('application.index');
27

28
		if ($https) $base = preg_replace('~http://~', 'https://', $base, 1);
29

30
		return rtrim($base, '/').'/'.ltrim($url, '/');
31 32 33 34 35
	}

	/**
	 * Generate an application URL with HTTPS.
	 *
36 37 38 39 40
	 * <code>
	 *		// Generate an application URL with HTTPS
	 *		echo URL::to_secure('user/profile');
	 * </code>
	 *
41 42 43
	 * @param  string  $url
	 * @return string
	 */
44
	public static function to_secure($url = '')
45
	{
46
		return static::to($url, true);
47 48 49
	}

	/**
50 51
	 * Generate an application URL to an asset.
	 *
52 53 54
	 * The index file will not be added to asset URLs. If the HTTPS option is not
	 * specified, HTTPS will be used when the active request is also using HTTPS.
	 *
55 56 57 58 59 60 61 62
	 * <code>
	 *		// Generate a URL to an asset
	 *		echo URL::to_asset('img/picture.jpg');
	 *
	 *		// Generate a URL to a an asset with HTTPS
	 *		echo URL::to_asset('img/picture.jpg', true);
	 * </code>
	 *
63
	 * @param  string  $url
64
	 * @param  bool    $https
65 66
	 * @return string
	 */
67
	public static function to_asset($url, $https = null)
68
	{
69
		if (is_null($https)) $https = Request::secure();
70

71
		return str_replace('index.php/', '', static::to($url, $https));
72 73 74
	}

	/**
75 76
	 * Generate a URL from a route name.
	 *
77 78 79
	 * For routes that have wildcard parameters, an array may be passed as the second
	 * parameter to the method. The values of this array will be used to fill the
	 * wildcard segments of the route URI.
80
	 *
81 82
	 * Optional parameters will be convereted to spaces if no parameter values are specified.
	 *
83 84 85 86 87 88 89 90
	 * <code>
	 *		// Generate the URL for a given route
	 *		echo URL::to_route('profile');
	 *
	 *		// Generate the URL for a given route with wildcard segments
	 *		echo URL::to_route('profile', array($username));
	 * </code>
	 *
91 92 93
	 * @param  string  $name
	 * @param  array   $parameters
	 * @param  bool    $https
94 95
	 * @return string
	 */
96
	public static function to_route($name, $parameters = array(), $https = false)
97
	{
98
		if ( ! is_null($route = IoC::container()->resolve('laravel.routing.router')->find($name)))
99
		{
100 101
			$uris = explode(', ', key($route));

102 103
			$uri = substr($uris[0], strpos($uris[0], '/'));

104 105
			// Spin through each route parameter and replace the route wildcard segment
			// with the corresponding parameter passed to the method.
106 107
			foreach ($parameters as $parameter)
			{
108
				$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
109 110
			}

111 112
			// Before generating the route URL, we will replace all remaining optional
			// wildcard segments that were not replaced by parameters with spaces.
113
			return static::to(str_replace(array('/(:any?)', '/(:num?)'), '', $uri), $https);
114 115 116 117 118 119
		}

		throw new \Exception("Error generating named route for route [$name]. Route is not defined.");
	}

	/**
120 121
	 * Generate a HTTPS URL from a route name.
	 *
122 123 124 125 126 127 128 129
	 * <code>
	 *		// Generate the URL for a route with HTTPS
	 *		echo URL::to_secure_route('profile');
	 *
	 *		// Generate the URL for a route with HTTPS and wildcard segments
	 *		echo URL::to_secure_route('profile', array($username));
	 * </code>
	 *
130 131 132 133
	 * @param  string  $name
	 * @param  array   $parameters
	 * @return string
	 */
134
	public static function to_secure_route($name, $parameters = array())
135
	{
136
		return static::to_route($name, $parameters, true);
137 138 139
	}

	/**
140 141 142 143 144 145
	 * Generate a URL friendly "slug".
	 *
	 * @param  string  $title
	 * @param  string  $separator
	 * @return string
	 */
146
	public static function slug($title, $separator = '-')
147
	{
148
		$title = Str::ascii($title);
149

150
		// Remove all characters that are not the separator, letters, numbers, or whitespace.
151 152
		$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));

153
		// Replace all separator characters and whitespace by a single separator
154 155 156 157 158 159
		$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);

		return trim($title, $separator);
	}

	/**
160
	 * Magic Method for dynamically creating URLs to named routes.
161 162 163 164 165 166 167 168 169 170 171
	 *
	 * <code>
	 *		// Generate the URL for the "profile" named route
	 *		echo URL::to_profile();
	 *
	 *		// Generate the URL for the "profile" named route with wildcard segments
	 *		echo URL::to_profile(array($username));
	 *
	 *		// Generate the URL for the "profile" named route with HTTPS
	 *		echo URL::to_secure_profile();
	 * </code>
172
	 */
173
	public static function __callStatic($method, $parameters)
174
	{
175 176
		$parameters = (isset($parameters[0])) ? $parameters[0] : array();

177 178
		if (strpos($method, 'to_secure_') === 0)
		{
179
			return static::to_route(substr($method, 10), $parameters, true);
180 181 182 183
		}

		if (strpos($method, 'to_') === 0)
		{
184
			return static::to_route(substr($method, 3), $parameters);
185 186 187 188 189 190
		}

		throw new \Exception("Method [$method] is not defined on the URL class.");
	}

}