uri.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * @param string $uri
  102. * @return bool
  103. */
  104. public static function is($pattern, $uri = null)
  105. {
  106. $uri = $uri ?: static::current();
  107. // Asterisks are translated into zero-or-more regular expression wildcards
  108. // to make it convenient to check if the URI starts with a given pattern
  109. // such as "library/*". This is only done when not root.
  110. if ($pattern !== '/')
  111. {
  112. $pattern = str_replace('*', '(.*)', $pattern).'\z';
  113. }
  114. else
  115. {
  116. $pattern = '^/$';
  117. }
  118. return preg_match('#'.$pattern.'#', $uri);
  119. }
  120. /**
  121. * Parse the PATH_INFO server variable.
  122. *
  123. * @param string $value
  124. * @return string
  125. */
  126. protected static function parse_path_info($value)
  127. {
  128. return $value;
  129. }
  130. /**
  131. * Parse the REQUEST_URI server variable.
  132. *
  133. * @param string $value
  134. * @return string
  135. */
  136. protected static function parse_request_uri($value)
  137. {
  138. return parse_url($value, PHP_URL_PATH);
  139. }
  140. /**
  141. * Parse the PHP_SELF server variable.
  142. *
  143. * @param string $value
  144. * @return string
  145. */
  146. protected static function parse_php_self($value)
  147. {
  148. return $value;
  149. }
  150. /**
  151. * Parse the REDIRECT_URL server variable.
  152. *
  153. * @param string $value
  154. * @return string
  155. */
  156. protected static function parse_redirect_url($value)
  157. {
  158. return $value;
  159. }
  160. /**
  161. * Remove the base URL off of the request URI.
  162. *
  163. * @param string $uri
  164. * @return string
  165. */
  166. protected static function remove_base($uri)
  167. {
  168. return static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
  169. }
  170. /**
  171. * Get a specific segment of the request URI via an one-based index.
  172. *
  173. * <code>
  174. * // Get the first segment of the request URI
  175. * $segment = URI::segment(1);
  176. *
  177. * // Get the second segment of the URI, or return a default value
  178. * $segment = URI::segment(2, 'Taylor');
  179. * </code>
  180. *
  181. * @param int $index
  182. * @param mixed $default
  183. * @return string
  184. */
  185. public static function segment($index, $default = null)
  186. {
  187. static::current();
  188. return array_get(static::$segments, $index - 1, $default);
  189. }
  190. /**
  191. * Set the URI segments for the request.
  192. *
  193. * @param string $uri
  194. * @return void
  195. */
  196. protected static function segments($uri)
  197. {
  198. $segments = explode('/', trim($uri, '/'));
  199. static::$segments = array_diff($segments, array(''));
  200. }
  201. /**
  202. * Remove a given value from the URI.
  203. *
  204. * @param string $uri
  205. * @param string $value
  206. * @return string
  207. */
  208. protected static function remove($uri, $value)
  209. {
  210. return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
  211. }
  212. /**
  213. * Get the query string for the current request.
  214. *
  215. * @return string
  216. */
  217. protected static function query()
  218. {
  219. return (count((array) $_GET) > 0) ? '?'.http_build_query($_GET) : '';
  220. }
  221. }