url.php 5.0 KB

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