url.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php namespace Laravel;
  2. class URL {
  3. /**
  4. * Generate an application URL.
  5. *
  6. * If the given URL is already well-formed, it will be returned unchanged.
  7. *
  8. * <code>
  9. * // Create a URL to a location within the application
  10. * $url = URL::to('user/profile');
  11. *
  12. * // Create a HTTPS URL to a location within the application
  13. * $url = URL::to('user/profile', true);
  14. * </code>
  15. *
  16. * @param string $url
  17. * @param bool $https
  18. * @return string
  19. */
  20. public static function to($url = '', $https = false)
  21. {
  22. if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
  23. $root = Config::$items['application']['url'].'/'.Config::$items['application']['index'];
  24. // Since SSL is often not used while developing the application, we allow the
  25. // developer to disable SSL on all framework generated links to make it more
  26. // convenient to work with the site while developing.
  27. if ($https and Config::$items['application']['ssl'])
  28. {
  29. $root = preg_replace('~http://~', 'https://', $root, 1);
  30. }
  31. return rtrim($root, '/').'/'.ltrim($url, '/');
  32. }
  33. /**
  34. * Generate an application URL with HTTPS.
  35. *
  36. * @param string $url
  37. * @return string
  38. */
  39. public static function to_secure($url = '')
  40. {
  41. return static::to($url, true);
  42. }
  43. /**
  44. * Generate an application URL to an asset.
  45. *
  46. * The index file will not be added to asset URLs. If the HTTPS option is not
  47. * specified, HTTPS will be used when the active request is also using HTTPS.
  48. *
  49. * @param string $url
  50. * @param bool $https
  51. * @return string
  52. */
  53. public static function to_asset($url, $https = null)
  54. {
  55. if (is_null($https)) $https = Request::secure();
  56. $url = static::to($url, $https);
  57. // Since assets are not served by Laravel, we do not need to come through
  58. // the front controller. We'll remove the application index specified in
  59. // the application configuration from the generated URL.
  60. if (($index = Config::$items['application']['index']) !== '')
  61. {
  62. $url = str_replace($index.'/', '', $url);
  63. }
  64. return $url;
  65. }
  66. /**
  67. * Generate a URL from a route name.
  68. *
  69. * For routes that have wildcard parameters, an array may be passed as the second
  70. * parameter to the method. The values of this array will be used to fill the
  71. * wildcard segments of the route URI.
  72. *
  73. * <code>
  74. * // Create a URL to the "profile" named route
  75. * $url = URL::to_route('profile');
  76. *
  77. * // Create a URL to the "profile" named route with wildcard parameters
  78. * $url = URL::to_route('profile', array($username));
  79. * </code>
  80. *
  81. * @param string $name
  82. * @param array $parameters
  83. * @param bool $https
  84. * @return string
  85. */
  86. public static function to_route($name, $parameters = array(), $https = false)
  87. {
  88. if ( ! is_null($route = IoC::core('routing.router')->find($name)))
  89. {
  90. $uris = explode(', ', key($route));
  91. // Grab the first URI assigned to the route and remove the URI and
  92. // leading slash from the destination that's defined on the route.
  93. $uri = substr($uris[0], strpos($uris[0], '/'));
  94. // Spin through each route parameter and replace the route wildcard
  95. // segment with the corresponding parameter passed to the method.
  96. foreach ((array) $parameters as $parameter)
  97. {
  98. $uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
  99. }
  100. // Replace all remaining optional segments with spaces. Since the
  101. // segments are optional, some of them may not have been assigned
  102. // values from the parameter array.
  103. return static::to(str_replace(array('/(:any?)', '/(:num?)'), '', $uri), $https);
  104. }
  105. throw new \OutOfBoundsException("Error getting URL for route [$name]. Route is not defined.");
  106. }
  107. /**
  108. * Generate a HTTPS URL from a route name.
  109. *
  110. * @param string $name
  111. * @param array $parameters
  112. * @return string
  113. */
  114. public static function to_secure_route($name, $parameters = array())
  115. {
  116. return static::to_route($name, $parameters, true);
  117. }
  118. /**
  119. * Generate a URL friendly "slug".
  120. *
  121. * <code>
  122. * // Returns "this-is-my-blog-post"
  123. * $slug = URL::slug('This is my blog post!');
  124. *
  125. * // Returns "this_is_my_blog_post"
  126. * $slug = URL::slug('This is my blog post!', '_');
  127. * </code>
  128. *
  129. * @param string $title
  130. * @param string $separator
  131. * @return string
  132. */
  133. public static function slug($title, $separator = '-')
  134. {
  135. $title = Str::ascii($title);
  136. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  137. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
  138. // Replace all separator characters and whitespace by a single separator
  139. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  140. return trim($title, $separator);
  141. }
  142. /**
  143. * Magic Method for dynamically creating URLs to named routes.
  144. *
  145. * <code>
  146. * // Create a URL to the "profile" named route
  147. * $url = URL::to_profile();
  148. *
  149. * // Create a URL to the "profile" named route with wildcard segments
  150. * $url = URL::to_profile(array($username));
  151. *
  152. * // Create a URL to the "profile" named route using HTTPS
  153. * $url = URL::to_secure_profile();
  154. * </code>
  155. */
  156. public static function __callStatic($method, $parameters)
  157. {
  158. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  159. if (strpos($method, 'to_secure_') === 0)
  160. {
  161. return static::to_route(substr($method, 10), $parameters, true);
  162. }
  163. if (strpos($method, 'to_') === 0)
  164. {
  165. return static::to_route(substr($method, 3), $parameters);
  166. }
  167. throw new \BadMethodCallException("Method [$method] is not defined on the URL class.");
  168. }
  169. }