uri.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public static $segments = array();
  15. /**
  16. * Get the full URI including the query string.
  17. *
  18. * @return string
  19. */
  20. public static function full()
  21. {
  22. return Request::getUri();
  23. }
  24. /**
  25. * Get the URI for the current request.
  26. *
  27. * @return string
  28. */
  29. public static function current()
  30. {
  31. if ( ! is_null(static::$uri)) return static::$uri;
  32. // We'll simply get the path info from the Symfony Request instance and then
  33. // format to meet our needs in the router. If the URI is root, we'll give
  34. // back a single slash, otherwise we'll strip the slashes.
  35. $uri = static::format(Request::getPathInfo());
  36. static::segments($uri);
  37. return static::$uri = $uri;
  38. }
  39. /**
  40. * Format a given URI.
  41. *
  42. * @param string $uri
  43. * @return string
  44. */
  45. protected static function format($uri)
  46. {
  47. return trim($uri, '/') ?: '/';
  48. }
  49. /**
  50. * Determine if the current URI matches a given pattern.
  51. *
  52. * @param string $pattern
  53. * @param string $uri
  54. * @return bool
  55. */
  56. public static function is($pattern, $uri = null)
  57. {
  58. $uri = $uri ?: static::current();
  59. // Asterisks are translated into zero-or-more regular expression wildcards
  60. // to make it convenient to check if the URI starts with a given pattern
  61. // such as "library/*". This is only done when not root.
  62. if ($pattern !== '/')
  63. {
  64. $pattern = str_replace('*', '(.*)', $pattern).'\z';
  65. }
  66. else
  67. {
  68. $pattern = '^/$';
  69. }
  70. return preg_match('#'.$pattern.'#', $uri);
  71. }
  72. /**
  73. * Get a specific segment of the request URI via an one-based index.
  74. *
  75. * <code>
  76. * // Get the first segment of the request URI
  77. * $segment = URI::segment(1);
  78. *
  79. * // Get the second segment of the URI, or return a default value
  80. * $segment = URI::segment(2, 'Taylor');
  81. * </code>
  82. *
  83. * @param int $index
  84. * @param mixed $default
  85. * @return string
  86. */
  87. public static function segment($index, $default = null)
  88. {
  89. static::current();
  90. return array_get(static::$segments, $index - 1, $default);
  91. }
  92. /**
  93. * Set the URI segments for the request.
  94. *
  95. * @param string $uri
  96. * @return void
  97. */
  98. protected static function segments($uri)
  99. {
  100. $segments = explode('/', trim($uri, '/'));
  101. static::$segments = array_diff($segments, array(''));
  102. }
  103. }