PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/laravel/uri.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 103 lines | 35 code | 15 blank | 53 comment | 3 complexity | 56ee40d3a471b024e6aa5444a0aae306 MD5 | raw file
  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. protected static $segments = array();
  15. /**
  16. * Get the URI for the current request.
  17. *
  18. * If the request is to the root of the application, a single forward slash
  19. * will be returned. Otherwise, the URI will be returned with all of the
  20. * leading and trailing slashes removed.
  21. *
  22. * @return string
  23. */
  24. public static function current()
  25. {
  26. if ( ! is_null(static::$uri)) return static::$uri;
  27. $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  28. // Remove the root application URL from the request URI. If the application
  29. // is nested within a sub-directory of the web document root, this will get
  30. // rid of all of the the sub-directories from the request URI.
  31. $uri = static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
  32. if (($index = '/'.Config::$items['application']['index']) !== '/')
  33. {
  34. $uri = static::remove($uri, $index);
  35. }
  36. static::$uri = static::format($uri);
  37. static::$segments = explode('/', static::$uri);
  38. return static::$uri;
  39. }
  40. /**
  41. * Get a specific segment of the request URI via an one-based index.
  42. *
  43. * <code>
  44. * // Get the first segment of the request URI
  45. * $segment = URI::segment(1);
  46. *
  47. * // Get the second segment of the URI, or return a default value
  48. * $segment = URI::segment(2, 'Taylor');
  49. * </code>
  50. *
  51. * @param int $index
  52. * @param mixed $default
  53. * @return string
  54. */
  55. public static function segment($index, $default = null)
  56. {
  57. static::current();
  58. return Arr::get(static::$segments, $index - 1, $default);
  59. }
  60. /**
  61. * Remove a given value from the URI.
  62. *
  63. * @param string $uri
  64. * @param string $value
  65. * @return string
  66. */
  67. protected static function remove($uri, $value)
  68. {
  69. if (strpos($uri, $value) === 0)
  70. {
  71. return substr($uri, strlen($value));
  72. }
  73. return $uri;
  74. }
  75. /**
  76. * Format a given URI.
  77. *
  78. * If the URI is an empty string, a single forward slash will be returned.
  79. * Otherwise, we will trim the URI's leading and trailing slashes.
  80. *
  81. * @param string $uri
  82. * @return string
  83. */
  84. protected static function format($uri)
  85. {
  86. return (($uri = trim($uri, '/')) !== '') ? $uri : '/';
  87. }
  88. }