url.php 5.1 KB

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