url.php 5.6 KB

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