uri.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. * The server variables to check for the URI.
  17. *
  18. * @var array
  19. */
  20. protected static $attempt = array(
  21. 'PATH_INFO', 'REQUEST_URI',
  22. 'PHP_SELF', 'REDIRECT_URL'
  23. );
  24. /**
  25. * Get the full URI including the query string.
  26. *
  27. * @return string
  28. */
  29. public static function full()
  30. {
  31. return static::current().static::query();
  32. }
  33. /**
  34. * Get the URI for the current request.
  35. *
  36. * @return string
  37. */
  38. public static function current()
  39. {
  40. if ( ! is_null(static::$uri)) return static::$uri;
  41. // To get the URI, we'll first call the detect method which will spin
  42. // through each of the server variables that we check for the URI in
  43. // and use the first one we encounter for the URI.
  44. static::$uri = static::detect();
  45. // If you ever encounter this error, please inform the nerdy Laravel
  46. // dev team with information about your server. We want to support
  47. // Laravel an as many servers as we possibly can!
  48. if (is_null(static::$uri))
  49. {
  50. throw new \Exception("Could not detect request URI.");
  51. }
  52. static::segments(static::$uri);
  53. return static::$uri;
  54. }
  55. /**
  56. * Detect the URI from the server variables.
  57. *
  58. * @return string|null
  59. */
  60. protected static function detect()
  61. {
  62. foreach (static::$attempt as $variable)
  63. {
  64. // Each variable we search for the URI has its own parser function
  65. // which is responsible for doing any formatting before the value
  66. // is fed into the main formatting function.
  67. $method = "parse_{$variable}";
  68. if (isset($_SERVER[$variable]))
  69. {
  70. $uri = static::$method($_SERVER[$variable]);
  71. return static::format($uri);
  72. }
  73. }
  74. }
  75. /**
  76. * Format a given URI.
  77. *
  78. * @param string $uri
  79. * @return string
  80. */
  81. protected static function format($uri)
  82. {
  83. // First we want to remove the application's base URL from the URI if it is
  84. // in the string. It is possible for some of the parsed server variables to
  85. // include the entire document root in the string.
  86. $uri = static::remove_base($uri);
  87. $index = '/'.Config::get('application.index');
  88. // Next we'll remove the index file from the URI if it is there and then
  89. // finally trim down the URI. If the URI is left with spaces, we'll use
  90. // a single slash for the root URI.
  91. if ($index !== '/')
  92. {
  93. $uri = static::remove($uri, $index);
  94. }
  95. return trim($uri, '/') ?: '/';
  96. }
  97. /**
  98. * Determine if the current URI matches a given pattern.
  99. *
  100. * @param string $pattern
  101. * @return bool
  102. */
  103. public static function is($pattern)
  104. {
  105. // Asterisks are translated into zero-or-more regular expression wildcards
  106. // to make it convenient to check if the URI starts with a given pattern
  107. // such as "library/*". This is only done when not root.
  108. if ($pattern !== '/')
  109. {
  110. $pattern = str_replace('*', '(.*)', $pattern).'\z';
  111. }
  112. else
  113. {
  114. $pattern = '^/$';
  115. }
  116. return preg_match('#'.$pattern.'#', static::current());
  117. }
  118. /**
  119. * Parse the PATH_INFO server variable.
  120. *
  121. * @param string $value
  122. * @return string
  123. */
  124. protected static function parse_path_info($value)
  125. {
  126. return $value;
  127. }
  128. /**
  129. * Parse the REQUEST_URI server variable.
  130. *
  131. * @param string $value
  132. * @return string
  133. */
  134. protected static function parse_request_uri($value)
  135. {
  136. return parse_url($value, PHP_URL_PATH);
  137. }
  138. /**
  139. * Parse the PHP_SELF server variable.
  140. *
  141. * @param string $value
  142. * @return string
  143. */
  144. protected static function parse_php_self($value)
  145. {
  146. return $value;
  147. }
  148. /**
  149. * Parse the REDIRECT_URL server variable.
  150. *
  151. * @param string $value
  152. * @return string
  153. */
  154. protected static function parse_redirect_url($value)
  155. {
  156. return $value;
  157. }
  158. /**
  159. * Remove the base URL off of the request URI.
  160. *
  161. * @param string $uri
  162. * @return string
  163. */
  164. protected static function remove_base($uri)
  165. {
  166. return static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
  167. }
  168. /**
  169. * Get a specific segment of the request URI via an one-based index.
  170. *
  171. * <code>
  172. * // Get the first segment of the request URI
  173. * $segment = URI::segment(1);
  174. *
  175. * // Get the second segment of the URI, or return a default value
  176. * $segment = URI::segment(2, 'Taylor');
  177. * </code>
  178. *
  179. * @param int $index
  180. * @param mixed $default
  181. * @return string
  182. */
  183. public static function segment($index, $default = null)
  184. {
  185. static::current();
  186. return array_get(static::$segments, $index - 1, $default);
  187. }
  188. /**
  189. * Set the URI segments for the request.
  190. *
  191. * @param string $uri
  192. * @return void
  193. */
  194. protected static function segments($uri)
  195. {
  196. $segments = explode('/', trim($uri, '/'));
  197. static::$segments = array_diff($segments, array(''));
  198. }
  199. /**
  200. * Remove a given value from the URI.
  201. *
  202. * @param string $uri
  203. * @param string $value
  204. * @return string
  205. */
  206. protected static function remove($uri, $value)
  207. {
  208. return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
  209. }
  210. /**
  211. * Get the query string for the current request.
  212. *
  213. * @return string
  214. */
  215. protected static function query()
  216. {
  217. return (count((array) $_GET) > 0) ? '?'.http_build_query($_GET) : '';
  218. }
  219. }