uri.php 3.0 KB

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