uri.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @return bool
  54. */
  55. public static function is($pattern)
  56. {
  57. return Str::is($pattern, static::current());
  58. }
  59. /**
  60. * Get a specific segment of the request URI via an one-based index.
  61. *
  62. * <code>
  63. * // Get the first segment of the request URI
  64. * $segment = URI::segment(1);
  65. *
  66. * // Get the second segment of the URI, or return a default value
  67. * $segment = URI::segment(2, 'Taylor');
  68. * </code>
  69. *
  70. * @param int $index
  71. * @param mixed $default
  72. * @return string
  73. */
  74. public static function segment($index, $default = null)
  75. {
  76. static::current();
  77. return array_get(static::$segments, $index - 1, $default);
  78. }
  79. /**
  80. * Set the URI segments for the request.
  81. *
  82. * @param string $uri
  83. * @return void
  84. */
  85. protected static function segments($uri)
  86. {
  87. $segments = explode('/', trim($uri, '/'));
  88. static::$segments = array_diff($segments, array(''));
  89. }
  90. }