url.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php namespace Laravel;
  2. class URL {
  3. /**
  4. * The cached base URL.
  5. *
  6. * @var string
  7. */
  8. public static $base;
  9. /**
  10. * Get the full URL for the current request.
  11. *
  12. * @return string
  13. */
  14. public static function current()
  15. {
  16. return static::to(URI::current());
  17. }
  18. /**
  19. * Get the base URL of the application.
  20. *
  21. * @return string
  22. */
  23. public static function base()
  24. {
  25. if (isset(static::$base)) return static::$base;
  26. $base = 'http://localhost';
  27. // If the application URL configuration is set, we will just use
  28. // that instead of trying to guess the URL based on the $_SERVER
  29. // array's host and script name.
  30. if (($url = Config::get('application.url')) !== '')
  31. {
  32. $base = $url;
  33. }
  34. elseif (isset($_SERVER['HTTP_HOST']))
  35. {
  36. $protocol = (Request::secure()) ? 'https://' : 'http://';
  37. // Basically, by removing the basename, we are removing everything after the
  38. // and including the front controller from the request URI. Leaving us with
  39. // the path in which the framework is installed. From that path, we can
  40. // construct the base URL to the application.
  41. $path = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  42. $base = rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/');
  43. }
  44. return static::$base = $base;
  45. }
  46. /**
  47. * Generate an application URL.
  48. *
  49. * <code>
  50. * // Create a URL to a location within the application
  51. * $url = URL::to('user/profile');
  52. *
  53. * // Create a HTTPS URL to a location within the application
  54. * $url = URL::to('user/profile', true);
  55. * </code>
  56. *
  57. * @param string $url
  58. * @param bool $https
  59. * @return string
  60. */
  61. public static function to($url = '', $https = false)
  62. {
  63. if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
  64. $root = static::base().'/'.Config::get('application.index');
  65. // Since SSL is not often used while developing the application, we allow the
  66. // developer to disable SSL on all framework generated links to make it more
  67. // convenient to work with the site while developing locally.
  68. if ($https and Config::get('application.ssl'))
  69. {
  70. $root = preg_replace('~http://~', 'https://', $root, 1);
  71. }
  72. return rtrim($root, '/').'/'.ltrim($url, '/');
  73. }
  74. /**
  75. * Generate an application URL with HTTPS.
  76. *
  77. * @param string $url
  78. * @return string
  79. */
  80. public static function to_secure($url = '')
  81. {
  82. return static::to($url, true);
  83. }
  84. /**
  85. * Generate a URL to a controller action.
  86. *
  87. * <code>
  88. * // Generate a URL to the "index" method of the "user" controller
  89. * $url = URL::to_action('user@index');
  90. *
  91. * // Generate a URL to http://example.com/user/profile/taylor
  92. * $url = URL::to_action('user@profile', array('taylor'));
  93. * </code>
  94. *
  95. * @param string $action
  96. * @param array $parameters
  97. * @param bool $https
  98. * @return string
  99. */
  100. public static function to_action($action, $parameters = array(), $https = false)
  101. {
  102. $action = str_replace(array('.', '@'), '/', $action);
  103. return static::to($action.'/'.implode('/', $parameters), $https);
  104. }
  105. /**
  106. * Generate a HTTPS URL to a controller action.
  107. *
  108. * @param string $action
  109. * @param array $parameters
  110. * @return string
  111. */
  112. public static function to_secure_action($action, $parameters = array())
  113. {
  114. return static::to_action($action, $parameters, true);
  115. }
  116. /**
  117. * Generate an application URL to an asset.
  118. *
  119. * @param string $url
  120. * @param bool $https
  121. * @return string
  122. */
  123. public static function to_asset($url, $https = null)
  124. {
  125. if (is_null($https)) $https = Request::secure();
  126. $url = static::to($url, $https);
  127. // Since assets are not served by Laravel, we do not need to come through
  128. // the front controller. So, we'll remove the application index specified
  129. // in the application configuration from the generated URL.
  130. if (($index = Config::get('application.index')) !== '')
  131. {
  132. $url = str_replace($index.'/', '', $url);
  133. }
  134. return $url;
  135. }
  136. /**
  137. * Generate a URL from a route name.
  138. *
  139. * <code>
  140. * // Create a URL to the "profile" named route
  141. * $url = URL::to_route('profile');
  142. *
  143. * // Create a URL to the "profile" named route with wildcard parameters
  144. * $url = URL::to_route('profile', array($username));
  145. * </code>
  146. *
  147. * @param string $name
  148. * @param array $parameters
  149. * @param bool $https
  150. * @return string
  151. */
  152. public static function to_route($name, $parameters = array(), $https = false)
  153. {
  154. if (is_null($route = Routing\Router::find($name)))
  155. {
  156. throw new \Exception("Error creating URL for undefined route [$name].");
  157. }
  158. $uris = explode(', ', key($route));
  159. // Routes can handle more than one URI, but we will just take the first URI
  160. // and use it for the URL. Since all of the URLs should point to the same
  161. // route, it doesn't make a difference.
  162. $uri = substr($uris[0], strpos($uris[0], '/'));
  163. // Spin through each route parameter and replace the route wildcard segment
  164. // with the corresponding parameter passed to the method. Afterwards, we'll
  165. // replace all of the remaining optional URI segments with spaces since
  166. // they may not have been specified in the array of parameters.
  167. foreach ((array) $parameters as $parameter)
  168. {
  169. $uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
  170. }
  171. // If there are any remaining optional place-holders, we'll just replace
  172. // them with empty strings since not every optional parameter has to be
  173. // in the array of parameters that were passed into the method.
  174. $uri = str_replace(array('/(:any?)', '/(:num?)'), '', $uri);
  175. return static::to($uri, $https);
  176. }
  177. /**
  178. * Generate a HTTPS URL from a route name.
  179. *
  180. * @param string $name
  181. * @param array $parameters
  182. * @return string
  183. */
  184. public static function to_secure_route($name, $parameters = array())
  185. {
  186. return static::to_route($name, $parameters, true);
  187. }
  188. }