url.php 5.1 KB

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