url.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php namespace Laravel; use Laravel\Routing\Router, Laravel\Routing\Route;
  2. class URL {
  3. /**
  4. * The cached base URL.
  5. *
  6. * @var string
  7. */
  8. public static $base;
  9. /**
  10. * Get the full URI including the query string.
  11. *
  12. * @return string
  13. */
  14. public static function full()
  15. {
  16. return static::to(URI::full());
  17. }
  18. /**
  19. * Get the full URL for the current request.
  20. *
  21. * @return string
  22. */
  23. public static function current()
  24. {
  25. return static::to(URI::current());
  26. }
  27. /**
  28. * Get the URL for the application root.
  29. *
  30. * @param bool $https
  31. * @return string
  32. */
  33. public static function home($https = false)
  34. {
  35. $route = Router::find('home');
  36. // If a route named "home" exists, we'll route to that instead of using
  37. // the single slash root URI. THis allows the HTTPS attribute to be
  38. // respected instead of being hard-coded in the redirect.
  39. if ( ! is_null($route))
  40. {
  41. return static::to_route('home');
  42. }
  43. return static::to('/', $https);
  44. }
  45. /**
  46. * Get the base URL of the application.
  47. *
  48. * @return string
  49. */
  50. public static function base()
  51. {
  52. if (isset(static::$base)) return static::$base;
  53. $base = 'http://localhost';
  54. // If the application URL configuration is set, we will just use that
  55. // instead of trying to guess the URL from the $_SERVER array's host
  56. // and script variables as this is more reliable.
  57. if (($url = Config::get('application.url')) !== '')
  58. {
  59. $base = $url;
  60. }
  61. elseif (isset($_SERVER['HTTP_HOST']))
  62. {
  63. $protocol = (Request::secure()) ? 'https://' : 'http://';
  64. // Basically, by removing the basename, we are removing everything after the
  65. // and including the front controller from the request URI. Leaving us with
  66. // the path in which the framework is installed.
  67. $script = $_SERVER['SCRIPT_NAME'];
  68. $path = str_replace(basename($script), '', $script);
  69. // Now that we have the base URL, all we need to do is attach the protocol
  70. // and the HTTP_HOST to build the full URL for the application. We also
  71. // trim off trailing slashes to clean the URL.
  72. $base = rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/');
  73. }
  74. return static::$base = $base;
  75. }
  76. /**
  77. * Generate an application URL.
  78. *
  79. * <code>
  80. * // Create a URL to a location within the application
  81. * $url = URL::to('user/profile');
  82. *
  83. * // Create a HTTPS URL to a location within the application
  84. * $url = URL::to('user/profile', true);
  85. * </code>
  86. *
  87. * @param string $url
  88. * @param bool $https
  89. * @return string
  90. */
  91. public static function to($url = '', $https = false)
  92. {
  93. if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
  94. $root = static::base().'/'.Config::get('application.index');
  95. // Since SSL is not often used while developing the application, we allow the
  96. // developer to disable SSL on all framework generated links to make it more
  97. // convenient to work with the site while developing locally.
  98. if ($https and Config::get('application.ssl'))
  99. {
  100. $root = preg_replace('~http://~', 'https://', $root, 1);
  101. }
  102. return rtrim($root, '/').'/'.ltrim($url, '/');
  103. }
  104. /**
  105. * Generate an application URL with HTTPS.
  106. *
  107. * @param string $url
  108. * @return string
  109. */
  110. public static function to_secure($url = '')
  111. {
  112. return static::to($url, true);
  113. }
  114. /**
  115. * Generate a URL to a controller action.
  116. *
  117. * <code>
  118. * // Generate a URL to the "index" method of the "user" controller
  119. * $url = URL::to_action('user@index');
  120. *
  121. * // Generate a URL to http://example.com/user/profile/taylor
  122. * $url = URL::to_action('user@profile', array('taylor'));
  123. * </code>
  124. *
  125. * @param string $action
  126. * @param array $parameters
  127. * @return string
  128. */
  129. public static function to_action($action, $parameters = array())
  130. {
  131. // This allows us to use true reverse routing to controllers, since
  132. // URIs may be setup to handle the action that do not follow the
  133. // typical Laravel controller URI conventions.
  134. $route = Router::uses($action);
  135. if ( ! is_null($route))
  136. {
  137. return static::explicit($route, $action, $parameters);
  138. }
  139. // If no route was found that handled the given action, we'll just
  140. // generate the URL using the typical controller routing setup
  141. // for URIs and turn SSL to false.
  142. else
  143. {
  144. return static::convention($action, $parameters);
  145. }
  146. }
  147. /**
  148. * Generate a action URL from a route definition
  149. *
  150. * @param array $route
  151. * @param string $action
  152. * @param array $parameters
  153. * @return string
  154. */
  155. protected static function explicit($route, $action, $parameters)
  156. {
  157. $https = array_get(current($route), 'https', false);
  158. return static::to(static::transpose(key($route), $parameters), $https);
  159. }
  160. /**
  161. * Generate an action URI by convention.
  162. *
  163. * @param string $action
  164. * @param array $parameters
  165. * @return string
  166. */
  167. protected static function convention($action, $parameters)
  168. {
  169. list($bundle, $action) = Bundle::parse($action);
  170. $bundle = Bundle::get($bundle);
  171. // If a bundle exists for the action, we will attempt to use it's "handles"
  172. // clause as the root of the generated URL, as the bundle can only handle
  173. // URIs that begin with that string.
  174. $root = $bundle['handles'] ?: '';
  175. $https = false;
  176. $parameters = implode('/', $parameters);
  177. // We'll replace both dots and @ signs in the URI since both are used
  178. // to specify the controller and action, and by convention should be
  179. // translated into URI slashes.
  180. $uri = $root.'/'.str_replace(array('.', '@'), '/', $action);
  181. $uri = static::to(str_finish($uri, '/').$parameters);
  182. return trim($uri, '/');
  183. }
  184. /**
  185. * Generate an application URL to an asset.
  186. *
  187. * @param string $url
  188. * @param bool $https
  189. * @return string
  190. */
  191. public static function to_asset($url, $https = null)
  192. {
  193. if (is_null($https)) $https = Request::secure();
  194. $url = static::to($url, $https);
  195. // Since assets are not served by Laravel, we do not need to come through
  196. // the front controller. So, we'll remove the application index specified
  197. // in the application config from the generated URL.
  198. if (($index = Config::get('application.index')) !== '')
  199. {
  200. $url = str_replace($index.'/', '', $url);
  201. }
  202. return $url;
  203. }
  204. /**
  205. * Generate a URL from a route name.
  206. *
  207. * <code>
  208. * // Create a URL to the "profile" named route
  209. * $url = URL::to_route('profile');
  210. *
  211. * // Create a URL to the "profile" named route with wildcard parameters
  212. * $url = URL::to_route('profile', array($username));
  213. * </code>
  214. *
  215. * @param string $name
  216. * @param array $parameters
  217. * @param bool $https
  218. * @return string
  219. */
  220. public static function to_route($name, $parameters = array())
  221. {
  222. if (is_null($route = Routing\Router::find($name)))
  223. {
  224. throw new \Exception("Error creating URL for undefined route [$name].");
  225. }
  226. // To determine whether the URL should be HTTPS or not, we look for the "https"
  227. // value on the route action array. The route has control over whether the URL
  228. // should be generated with an HTTPS protocol string or just HTTP.
  229. $https = array_get(current($route), 'https', false);
  230. $uri = trim(static::transpose(key($route), $parameters), '/');
  231. return static::to($uri, $https);
  232. }
  233. /**
  234. * Substitute the parameters in a given URI.
  235. *
  236. * @param string $uri
  237. * @param array $parameters
  238. * @return string
  239. */
  240. public static function transpose($uri, $parameters)
  241. {
  242. // Spin through each route parameter and replace the route wildcard segment
  243. // with the corresponding parameter passed to the method. Afterwards, we'll
  244. // replace all of the remaining optional URI segments.
  245. foreach ((array) $parameters as $parameter)
  246. {
  247. if ( ! is_null($parameter))
  248. {
  249. $uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
  250. }
  251. }
  252. // If there are any remaining optional place-holders, we'll just replace
  253. // them with empty strings since not every optional parameter has to be
  254. // in the array of parameters that were passed.
  255. $uri = str_replace(array_keys(Router::$optional), '', $uri);
  256. return trim($uri, '/');
  257. }
  258. }