url.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php namespace Laravel;
  2. class URL {
  3. /**
  4. * The application router instance.
  5. *
  6. * @var Routing\Router
  7. */
  8. protected $router;
  9. /**
  10. * The base URL of the application.
  11. *
  12. * @var string
  13. */
  14. protected $base;
  15. /**
  16. * The application index file.
  17. *
  18. * @var string
  19. */
  20. protected $index;
  21. /**
  22. * Indicates if the current request is using HTTPS.
  23. *
  24. * @var bool
  25. */
  26. protected $https;
  27. /**
  28. * Create a new URL writer instance.
  29. *
  30. * @param Routing\Router $router
  31. * @param string $base
  32. * @param string $index
  33. * @param bool $https
  34. * @return void
  35. */
  36. public function __construct(Routing\Router $router, $base, $index, $https)
  37. {
  38. $this->base = $base;
  39. $this->https = $https;
  40. $this->index = $index;
  41. $this->router = $router;
  42. }
  43. /**
  44. * Generate an application URL.
  45. *
  46. * If the given URL is already well-formed, it will be returned unchanged.
  47. *
  48. * <code>
  49. * // Generate an application URL from a given URI
  50. * echo URL::to('user/profile');
  51. *
  52. * // Generate an application URL with HTTPS
  53. * echo URL::to('user/profile', true);
  54. * </code>
  55. *
  56. * @param string $url
  57. * @param bool $https
  58. * @return string
  59. */
  60. public function to($url = '', $https = false)
  61. {
  62. if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
  63. $base = $this->base.'/'.$this->index;
  64. if ($https) $base = preg_replace('~http://~', 'https://', $base, 1);
  65. return rtrim($base, '/').'/'.ltrim($url, '/');
  66. }
  67. /**
  68. * Generate an application URL with HTTPS.
  69. *
  70. * <code>
  71. * // Generate an application URL with HTTPS
  72. * echo URL::to_secure('user/profile');
  73. * </code>
  74. *
  75. * @param string $url
  76. * @return string
  77. */
  78. public function to_secure($url = '')
  79. {
  80. return $this->to($url, true);
  81. }
  82. /**
  83. * Generate an application URL to an asset.
  84. *
  85. * The index file will not be added to asset URLs. If the HTTPS option is not
  86. * specified, HTTPS will be used when the active request is also using HTTPS.
  87. *
  88. * <code>
  89. * // Generate a URL to an asset
  90. * echo URL::to_asset('img/picture.jpg');
  91. *
  92. * // Generate a URL to a an asset with HTTPS
  93. * echo URL::to_asset('img/picture.jpg', true);
  94. * </code>
  95. *
  96. * @param string $url
  97. * @param bool $https
  98. * @return string
  99. */
  100. public function to_asset($url, $https = null)
  101. {
  102. if (is_null($https)) $https = $this->https;
  103. return str_replace('index.php/', '', $this->to($url, $https));
  104. }
  105. /**
  106. * Generate a URL from a route name.
  107. *
  108. * For routes that have wildcard parameters, an array may be passed as the second
  109. * parameter to the method. The values of this array will be used to fill the
  110. * wildcard segments of the route URI.
  111. *
  112. * Optional parameters will be convereted to spaces if no parameter values are specified.
  113. *
  114. * <code>
  115. * // Generate the URL for a given route
  116. * echo URL::to_route('profile');
  117. *
  118. * // Generate the URL for a given route with wildcard segments
  119. * echo URL::to_route('profile', array($username));
  120. * </code>
  121. *
  122. * @param string $name
  123. * @param array $parameters
  124. * @param bool $https
  125. * @return string
  126. */
  127. public function to_route($name, $parameters = array(), $https = false)
  128. {
  129. if ( ! is_null($route = $this->router->find($name)))
  130. {
  131. $uris = explode(', ', key($route));
  132. $uri = substr($uris[0], strpos($uris[0], '/'));
  133. foreach ($parameters as $parameter)
  134. {
  135. $uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
  136. }
  137. return $this->to(str_replace(array('/(:any?)', '/(:num?)'), '', $uri), $https);
  138. }
  139. throw new \Exception("Error generating named route for route [$name]. Route is not defined.");
  140. }
  141. /**
  142. * Generate a HTTPS URL from a route name.
  143. *
  144. * <code>
  145. * // Generate the URL for a route with HTTPS
  146. * echo URL::to_secure_route('profile');
  147. *
  148. * // Generate the URL for a route with HTTPS and wildcard segments
  149. * echo URL::to_secure_route('profile', array($username));
  150. * </code>
  151. *
  152. * @param string $name
  153. * @param array $parameters
  154. * @return string
  155. */
  156. public function to_secure_route($name, $parameters = array())
  157. {
  158. return $this->to_route($name, $parameters, true);
  159. }
  160. /**
  161. * Generate a URL friendly "slug".
  162. *
  163. * @param string $title
  164. * @param string $separator
  165. * @return string
  166. */
  167. public function slug($title, $separator = '-')
  168. {
  169. $title = Str::ascii($title);
  170. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  171. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
  172. // Replace all separator characters and whitespace by a single separator
  173. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  174. return trim($title, $separator);
  175. }
  176. /**
  177. * Magic Method for dynamically creating URLs to named routes.
  178. *
  179. * <code>
  180. * // Generate the URL for the "profile" named route
  181. * echo URL::to_profile();
  182. *
  183. * // Generate the URL for the "profile" named route with wildcard segments
  184. * echo URL::to_profile(array($username));
  185. *
  186. * // Generate the URL for the "profile" named route with HTTPS
  187. * echo URL::to_secure_profile();
  188. * </code>
  189. */
  190. public function __call($method, $parameters)
  191. {
  192. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  193. if (strpos($method, 'to_secure_') === 0)
  194. {
  195. return $this->to_route(substr($method, 10), $parameters, true);
  196. }
  197. if (strpos($method, 'to_') === 0)
  198. {
  199. return $this->to_route(substr($method, 3), $parameters);
  200. }
  201. throw new \Exception("Method [$method] is not defined on the URL class.");
  202. }
  203. }