url.php 5.5 KB

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