uri.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 URI for the current request.
  17. *
  18. * @return string
  19. */
  20. public static function current()
  21. {
  22. if ( ! is_null(static::$uri)) return static::$uri;
  23. $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  24. // Remove the root application URL from the request URI. If the application
  25. // is nested within a sub-directory of the web document root, this will get
  26. // rid of all of the the sub-directories from the request URI.
  27. $uri = static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
  28. // We'll also remove the application's index page as it is not used for at
  29. // all for routing and is totally unnecessary as far as the framework is
  30. // concerned. It is only in the URI when mod_rewrite is not available.
  31. if (($index = '/'.Config::get('application.index')) !== '/')
  32. {
  33. $uri = static::remove($uri, $index);
  34. }
  35. static::$uri = static::format($uri);
  36. // Cache the URI segments. This allows us to avoid having to explode
  37. // the segments every time the developer requests one of them. The
  38. // extra slashes have already been stripped off of the URI so no
  39. // extraneous elements should be present in the segment array.
  40. $segments = explode('/', trim(static::$uri, '/'));
  41. static::$segments = array_diff($segments, array(''));
  42. return static::$uri;
  43. }
  44. /**
  45. * Get a specific segment of the request URI via an one-based index.
  46. *
  47. * <code>
  48. * // Get the first segment of the request URI
  49. * $segment = URI::segment(1);
  50. *
  51. * // Get the second segment of the URI, or return a default value
  52. * $segment = URI::segment(2, 'Taylor');
  53. * </code>
  54. *
  55. * @param int $index
  56. * @param mixed $default
  57. * @return string
  58. */
  59. public static function segment($index, $default = null)
  60. {
  61. static::current();
  62. return array_get(static::$segments, $index - 1, $default);
  63. }
  64. /**
  65. * Remove a given value from the URI.
  66. *
  67. * @param string $uri
  68. * @param string $value
  69. * @return string
  70. */
  71. protected static function remove($uri, $value)
  72. {
  73. return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
  74. }
  75. /**
  76. * Format a given URI.
  77. *
  78. * @param string $uri
  79. * @return string
  80. */
  81. protected static function format($uri)
  82. {
  83. return (($uri = trim($uri, '/')) !== '') ? $uri : '/';
  84. }
  85. }