url.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php namespace Laravel;
  2. class URL {
  3. /**
  4. * Get the base URL of the application.
  5. *
  6. * If the application URL is explicitly defined in the application configuration
  7. * file, that URL will be returned. Otherwise, the URL will be guessed based on
  8. * the host and script name available in the global $_SERVER array.
  9. *
  10. * @return string
  11. */
  12. public static function base()
  13. {
  14. if (($base = Config::$items['application']['url']) !== '') return $base;
  15. if (isset($_SERVER['HTTP_HOST']))
  16. {
  17. $protocol = (Request::secure()) ? 'https://' : 'http://';
  18. // By removing the basename of the script, we should be left with the path
  19. // in which the framework is installed. For example, if the framework is
  20. // installed to http://localhost/laravel/public, the path we'll get from
  21. // this statement will be "/laravel/public".
  22. $path = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  23. return rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/');
  24. }
  25. return 'http://localhost';
  26. }
  27. /**
  28. * Generate an application URL.
  29. *
  30. * <code>
  31. * // Create a URL to a location within the application
  32. * $url = URL::to('user/profile');
  33. *
  34. * // Create a HTTPS URL to a location within the application
  35. * $url = URL::to('user/profile', true);
  36. * </code>
  37. *
  38. * @param string $url
  39. * @param bool $https
  40. * @return string
  41. */
  42. public static function to($url = '', $https = false)
  43. {
  44. if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
  45. $root = static::base().'/'.Config::$items['application']['index'];
  46. // Since SSL is often not used while developing the application, we allow the
  47. // developer to disable SSL on all framework generated links to make it more
  48. // convenient to work with the site while developing.
  49. if ($https and Config::$items['application']['ssl'])
  50. {
  51. $root = preg_replace('~http://~', 'https://', $root, 1);
  52. }
  53. return rtrim($root, '/').'/'.ltrim($url, '/');
  54. }
  55. /**
  56. * Generate an application URL with HTTPS.
  57. *
  58. * @param string $url
  59. * @return string
  60. */
  61. public static function to_secure($url = '')
  62. {
  63. return static::to($url, true);
  64. }
  65. /**
  66. * Generate an application URL to an asset.
  67. *
  68. * @param string $url
  69. * @param bool $https
  70. * @return string
  71. */
  72. public static function to_asset($url, $https = null)
  73. {
  74. if (is_null($https)) $https = Request::secure();
  75. $url = static::to($url, $https);
  76. // Since assets are not served by Laravel, we do not need to come through
  77. // the front controller. We'll remove the application index specified in
  78. // the application configuration from the generated URL.
  79. if (($index = Config::$items['application']['index']) !== '')
  80. {
  81. $url = str_replace($index.'/', '', $url);
  82. }
  83. return $url;
  84. }
  85. /**
  86. * Generate a URL from a route name.
  87. *
  88. * For routes that have wildcard parameters, an array may be passed as the
  89. * second parameter to the method. The values of this array will be used to
  90. * fill the wildcard segments of the route URI.
  91. *
  92. * <code>
  93. * // Create a URL to the "profile" named route
  94. * $url = URL::to_route('profile');
  95. *
  96. * // Create a URL to the "profile" named route with wildcard parameters
  97. * $url = URL::to_route('profile', array($username));
  98. * </code>
  99. *
  100. * @param string $name
  101. * @param array $parameters
  102. * @param bool $https
  103. * @return string
  104. */
  105. public static function to_route($name, $parameters = array(), $https = false)
  106. {
  107. if ( ! is_null($route = IoC::core('routing.router')->find($name)))
  108. {
  109. $uris = explode(', ', key($route));
  110. $uri = substr($uris[0], strpos($uris[0], '/'));
  111. // Spin through each route parameter and replace the route wildcard
  112. // segment with the corresponding parameter passed to the method.
  113. // Afterwards, we will replace all of the remaining optional URI
  114. // segments with spaces since they may not have been specified
  115. // in the array of parameters.
  116. foreach ((array) $parameters as $parameter)
  117. {
  118. $uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
  119. }
  120. return static::to(str_replace(array('/(:any?)', '/(:num?)'), '', $uri), $https);
  121. }
  122. throw new \OutOfBoundsException("Error creating URL for undefined route [$name].");
  123. }
  124. /**
  125. * Generate a HTTPS URL from a route name.
  126. *
  127. * @param string $name
  128. * @param array $parameters
  129. * @return string
  130. */
  131. public static function to_secure_route($name, $parameters = array())
  132. {
  133. return static::to_route($name, $parameters, true);
  134. }
  135. /**
  136. * Generate a URL to a controller action.
  137. *
  138. * <code>
  139. * // Generate a URL to the "index" method of the "user" controller
  140. * $url = URL::to_action('user@index');
  141. *
  142. * // Generate a URL to http://example.com/user/profile/taylor
  143. * $url = URL::to_action('user@profile', array('taylor'));
  144. * </code>
  145. *
  146. * @param string $action
  147. * @param array $parameters
  148. * @param bool $https
  149. * @return string
  150. */
  151. public static function to_action($action, $parameters = array(), $https = false)
  152. {
  153. $action = str_replace(array('.', '@'), '/', $action);
  154. return static::to($action.'/'.implode('/', $parameters), $https);
  155. }
  156. /**
  157. * Generate a HTTPS URL to a controller action.
  158. *
  159. * <code>
  160. * // Generate a HTTPS URL to the "index" method of the "user" controller
  161. * $url = URL::to_action('user@index');
  162. * </code>
  163. *
  164. * @param string $action
  165. * @param array $parameters
  166. * @param bool $https
  167. * @return string
  168. */
  169. public static function to_secure_action($action, $parameters = array())
  170. {
  171. return static::to_action($action, $parameters, true);
  172. }
  173. /**
  174. * Generate a URL friendly "slug".
  175. *
  176. * <code>
  177. * // Returns "this-is-my-blog-post"
  178. * $slug = URL::slug('This is my blog post!');
  179. *
  180. * // Returns "this_is_my_blog_post"
  181. * $slug = URL::slug('This is my blog post!', '_');
  182. * </code>
  183. *
  184. * @param string $title
  185. * @param string $separator
  186. * @return string
  187. */
  188. public static function slug($title, $separator = '-')
  189. {
  190. $title = Str::ascii($title);
  191. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  192. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
  193. // Replace all separator characters and whitespace by a single separator
  194. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  195. return trim($title, $separator);
  196. }
  197. /**
  198. * Magic Method for dynamically creating URLs to named routes.
  199. *
  200. * <code>
  201. * // Create a URL to the "profile" named route
  202. * $url = URL::to_profile();
  203. *
  204. * // Create a URL to the "profile" named route with wildcard segments
  205. * $url = URL::to_profile(array($username));
  206. *
  207. * // Create a URL to the "profile" named route using HTTPS
  208. * $url = URL::to_secure_profile();
  209. * </code>
  210. */
  211. public static function __callStatic($method, $parameters)
  212. {
  213. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  214. if (strpos($method, 'to_secure_') === 0)
  215. {
  216. return static::to_route(substr($method, 10), $parameters, true);
  217. }
  218. if (strpos($method, 'to_') === 0)
  219. {
  220. return static::to_route(substr($method, 3), $parameters);
  221. }
  222. throw new \BadMethodCallException("Method [$method] is not defined on the URL class.");
  223. }
  224. }