url.php 5.0 KB

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