uri.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php namespace Laravel;
  2. class URI {
  3. /**
  4. * The URI for the current request.
  5. *
  6. * This property will be set after the URI is detected for the first time.
  7. *
  8. * @var string
  9. */
  10. protected static $uri;
  11. /**
  12. * Determine the request URI.
  13. *
  14. * The request URI will be trimmed to remove to the application URL and application index file.
  15. * If the request is to the root of the application, the URI will be set to a forward slash.
  16. *
  17. * If the $_SERVER "PATH_INFO" variable is available, it will be used; otherwise, we will try
  18. * to determine the URI using the REQUEST_URI variable. If neither are available, an exception
  19. * will be thrown by the method.
  20. *
  21. * @return string
  22. */
  23. public static function get()
  24. {
  25. if ( ! is_null(static::$uri)) return static::$uri;
  26. if (($uri = static::from_server()) === false)
  27. {
  28. throw new \Exception('Malformed request URI. Request terminated.');
  29. }
  30. return static::$uri = static::format(static::clean($uri));
  31. }
  32. /**
  33. * Get a given URI segment from the URI for the current request.
  34. *
  35. * <code>
  36. * // Get the first segment from the request URI
  37. * $first = Request::uri()->segment(1);
  38. *
  39. * // Get the second segment or return a default value if it doesn't exist
  40. * $second = Request::uri()->segment(2, 'Taylor');
  41. *
  42. * // Get all of the segments for the request URI
  43. * $segments = Request::uri()->segment();
  44. * </code>
  45. *
  46. * @param int $segment
  47. * @param mixed $default
  48. * @return string
  49. */
  50. public static function segment($segment = null, $default = null)
  51. {
  52. $segments = Arr::without(explode('/', static::get()), array(''));
  53. if ( ! is_null($segment)) $segment = $segment - 1;
  54. return Arr::get($segments, $segment, $default);
  55. }
  56. /**
  57. * Get the request URI from the $_SERVER array.
  58. *
  59. * @return string
  60. */
  61. protected static function from_server()
  62. {
  63. // If the PATH_INFO $_SERVER element is set, we will use since it contains
  64. // the request URI formatted perfectly for Laravel's routing engine.
  65. if (isset($_SERVER['PATH_INFO']))
  66. {
  67. return $_SERVER['PATH_INFO'];
  68. }
  69. // If the REQUEST_URI is set, we need to extract the URL path since this
  70. // should return the URI formatted in a manner similar to PATH_INFO.
  71. elseif (isset($_SERVER['REQUEST_URI']))
  72. {
  73. return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  74. }
  75. throw new \Exception('Unable to determine the request URI.');
  76. }
  77. /**
  78. * Remove extraneous segments from the URI such as the URL and index page.
  79. *
  80. * These segments need to be removed since they will cause problems in the
  81. * routing engine if they are present in the URI.
  82. *
  83. * @param string $uri
  84. * @return string
  85. */
  86. protected static function clean($uri)
  87. {
  88. foreach (array(parse_url(Config::get('application.url'), PHP_URL_PATH), '/index.php') as $value)
  89. {
  90. $uri = (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
  91. }
  92. return $uri;
  93. }
  94. /**
  95. * Format the URI.
  96. *
  97. * If the request URI is empty, a single forward slash will be returned.
  98. *
  99. * @param string $uri
  100. * @return string
  101. */
  102. protected static function format($uri)
  103. {
  104. return (($uri = trim($uri, '/')) == '') ? '/' : $uri;
  105. }
  106. }