url.php 3.8 KB

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