uri.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php namespace Laravel;
  2. class URI {
  3. /**
  4. * The URI for the current request.
  5. *
  6. * @var string
  7. */
  8. public static $uri;
  9. /**
  10. * The URI segments for the current request.
  11. *
  12. * @var array
  13. */
  14. protected static $segments = array();
  15. /**
  16. * Get the URI for the current request.
  17. *
  18. * If the request is to the root of the application, a single forward slash
  19. * will be returned. Otherwise, the URI will be returned with all of the
  20. * leading and trailing slashes removed.
  21. *
  22. * @return string
  23. */
  24. public static function current()
  25. {
  26. if ( ! is_null(static::$uri)) return static::$uri;
  27. $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  28. // Remove the root application URL from the request URI. If the application
  29. // is nested within a sub-directory of the web document root, this will get
  30. // rid of all of the the sub-directories from the request URI.
  31. $uri = static::remove($uri, parse_url(Config::$items['application']['url'], PHP_URL_PATH));
  32. if (($index = '/'.Config::$items['application']['index']) !== '/')
  33. {
  34. $uri = static::remove($uri, $index);
  35. }
  36. // Format the final request URI. If there is nothing left, we will just
  37. // return a single forward slash. Otherwise, we'll remove all of the
  38. // leading and trailing spaces from the URI before returning it.
  39. static::$uri = static::format($uri);
  40. static::$segments = explode('/', static::$uri);
  41. return static::$uri;
  42. }
  43. /**
  44. * Get a specific segment of the request URI via an one-based index.
  45. *
  46. * <code>
  47. * // Get the first segment of the request URI
  48. * $segment = URI::segment(1);
  49. *
  50. * // Get the second segment of the URI, or return a default value
  51. * $segment = URI::segment(2, 'Taylor');
  52. * </code>
  53. *
  54. * @param int $index
  55. * @param mixed $default
  56. * @return string
  57. */
  58. public static function segment($index, $default = null)
  59. {
  60. static::current();
  61. return Arr::get(static::$segments, $index - 1, $default);
  62. }
  63. /**
  64. * Remove a given value from the URI.
  65. *
  66. * @param string $uri
  67. * @param string $value
  68. * @return string
  69. */
  70. protected static function remove($uri, $value)
  71. {
  72. if (strpos($uri, $value) === 0)
  73. {
  74. return substr($uri, strlen($value));
  75. }
  76. return $uri;
  77. }
  78. /**
  79. * Format a given URI.
  80. *
  81. * If the URI is an empty string, a single forward slash will be returned.
  82. * Otherwise, we will simply trim the URI's leading and trailing slashes.
  83. *
  84. * @param string $uri
  85. * @return string
  86. */
  87. protected static function format($uri)
  88. {
  89. return (($uri = trim($uri, '/')) !== '') ? $uri : '/';
  90. }
  91. }