uri.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php namespace Laravel;
  2. class URI {
  3. /**
  4. * The request URI for the current request.
  5. *
  6. * @var string
  7. */
  8. protected $uri;
  9. /**
  10. * The $_SERVER global array for the current request.
  11. *
  12. * @var array
  13. */
  14. protected $server;
  15. /**
  16. * Create a new instance of the URI class.
  17. *
  18. * @param array $server
  19. * @return void
  20. */
  21. public function __construct($server)
  22. {
  23. $this->server = $server;
  24. }
  25. /**
  26. * Get the request URI for the current request.
  27. *
  28. * If the request is to the root of the application, a single forward slash
  29. * will be returned. Otherwise, the URI will be returned with all leading
  30. * and trailing slashes removed. The application URL and index file will
  31. * also be removed since they are not used when routing the request.
  32. *
  33. * @return string
  34. */
  35. public function get()
  36. {
  37. if ( ! is_null($this->uri)) return $this->uri;
  38. return $this->uri = $this->format($this->clean($this->parse($this->server['REQUEST_URI'])));
  39. }
  40. /**
  41. * Remove extraneous information from the given request URI.
  42. *
  43. * @param string $uri
  44. * @return string
  45. */
  46. protected function clean($uri)
  47. {
  48. $uri = $this->remove($uri, $this->parse(Config::$items['application']['url']));
  49. if (($index = '/'.Config::$items['application']['index']) !== '/')
  50. {
  51. $uri = $this->remove($uri, $index);
  52. }
  53. return $uri;
  54. }
  55. /**
  56. * Parse a given string URI using PHP_URL_PATH to remove the domain.
  57. *
  58. * @return string
  59. */
  60. protected function parse($uri)
  61. {
  62. return parse_url($uri, PHP_URL_PATH);
  63. }
  64. /**
  65. * Remove a string from the beginning of a URI.
  66. *
  67. * @param string $uri
  68. * @param string $remove
  69. * @return string
  70. */
  71. protected function remove($uri, $remove)
  72. {
  73. return (strpos($uri, $remove) === 0) ? substr($uri, strlen($remove)) : $uri;
  74. }
  75. /**
  76. * Format the URI for use throughout the framework.
  77. *
  78. * @param string $uri
  79. * @return string
  80. */
  81. protected function format($uri)
  82. {
  83. return (($uri = trim($uri, '/')) !== '') ? $uri : '/';
  84. }
  85. }