url.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php namespace Laravel;
  2. class URL {
  3. /**
  4. * Create a new URL writer instance.
  5. *
  6. * @param Router $router
  7. * @param string $base
  8. * @param string $index
  9. * @param bool $https
  10. * @return void
  11. */
  12. public function __construct(Routing\Router $router, $base, $index, $https)
  13. {
  14. $this->base = $base;
  15. $this->https = $https;
  16. $this->index = $index;
  17. $this->router = $router;
  18. }
  19. /**
  20. * Generate an application URL.
  21. *
  22. * If the given URL is already well-formed, it will be returned unchanged.
  23. *
  24. * @param string $url
  25. * @param bool $https
  26. * @return string
  27. */
  28. public function to($url = '', $https = false)
  29. {
  30. if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
  31. $base = $this->base.'/'.$this->index;
  32. if ($https) $base = preg_replace('~http://~', 'https://', $base, 1);
  33. return rtrim($base, '/').'/'.trim($url, '/');
  34. }
  35. /**
  36. * Generate an application URL with HTTPS.
  37. *
  38. * @param string $url
  39. * @return string
  40. */
  41. public function to_secure($url = '')
  42. {
  43. return $this->to($url, true);
  44. }
  45. /**
  46. * Generate an application URL to an asset.
  47. *
  48. * The index file will not be added to asset URLs. If the HTTPS option is not
  49. * specified, HTTPS will be used when the active request is also using HTTPS.
  50. *
  51. * @param string $url
  52. * @param bool $https
  53. * @return string
  54. */
  55. public function to_asset($url, $https = null)
  56. {
  57. if (is_null($https)) $https = $this->https;
  58. return str_replace('index.php/', '', $this->to($url, $https));
  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. * Optional parameters will be convereted to spaces if no parameter values are specified.
  68. *
  69. * @param string $name
  70. * @param array $parameters
  71. * @param bool $https
  72. * @return string
  73. */
  74. public function to_route($name, $parameters = array(), $https = false)
  75. {
  76. if ( ! is_null($route = $this->router->find($name)))
  77. {
  78. $uris = explode(', ', key($route));
  79. $uri = substr($uris[0], strpos($uris[0], '/'));
  80. foreach ($parameters as $parameter)
  81. {
  82. $uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
  83. }
  84. return $this->to(str_replace(array('/(:any?)', '/(:num?)'), '', $uri), $https);
  85. }
  86. throw new \Exception("Error generating named route for route [$name]. Route is not defined.");
  87. }
  88. /**
  89. * Generate a HTTPS URL from a route name.
  90. *
  91. * @param string $name
  92. * @param array $parameters
  93. * @return string
  94. */
  95. public function to_secure_route($name, $parameters = array())
  96. {
  97. return $this->to_route($name, $parameters, true);
  98. }
  99. /**
  100. * Generate a URL friendly "slug".
  101. *
  102. * @param string $title
  103. * @param string $separator
  104. * @return string
  105. */
  106. public function slug($title, $separator = '-')
  107. {
  108. $title = Str::ascii($title);
  109. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  110. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', Str::lower($title));
  111. // Replace all separator characters and whitespace by a single separator
  112. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  113. return trim($title, $separator);
  114. }
  115. /**
  116. * Magic Method for dynamically creating URLs to named routes.
  117. */
  118. public function __call($method, $parameters)
  119. {
  120. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  121. if (strpos($method, 'to_secure_') === 0)
  122. {
  123. return $this->to_route(substr($method, 10), $parameters, true);
  124. }
  125. if (strpos($method, 'to_') === 0)
  126. {
  127. return $this->to_route(substr($method, 3), $parameters);
  128. }
  129. throw new \Exception("Method [$method] is not defined on the URL class.");
  130. }
  131. }