url.php 5.2 KB

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