url.php 4.7 KB

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