diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index fca059a3f..001a448b4 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -28,6 +28,7 @@ class Uri protected $scheme; protected $port; protected $query; + protected $fragment; protected $root; protected $root_path; protected $uri; @@ -196,6 +197,7 @@ class Uri $this->host = []; $this->root = []; $this->url = []; + $this->fragment = []; $grav = Grav::instance(); @@ -220,6 +222,10 @@ class Uri parse_str($uri_bits['query'], $this->query); } + if (isset($uri_bits['fragment'])) { + $this->fragment = $uri_bits['fragment']; + } + $this->base = $this->buildBaseUrl(); $this->host = $this->buildHost(); $this->env = $this->buildEnvironment(); @@ -300,6 +306,11 @@ class Uri $uri = $bits['path']; } + //process fragment + if (isset($bits['fragment'])) { + $this->fragment = $bits['fragment']; + } + // remove the extension if there is one set $parts = pathinfo($uri); @@ -464,6 +475,21 @@ class Uri } } + /** + * Gets the Fragment portion of a URI (eg #target) + * + * @param null $fragment + * + * @return null + */ + public function fragment($fragment = null) + { + if ($fragment !== null) { + $this->fragment = $fragment; + } + return $this->fragment; + } + /** * Return URL. * diff --git a/tests/unit/Grav/Common/UriTest.php b/tests/unit/Grav/Common/UriTest.php index d3d4853c9..70887da46 100644 --- a/tests/unit/Grav/Common/UriTest.php +++ b/tests/unit/Grav/Common/UriTest.php @@ -177,6 +177,15 @@ class UriTest extends \Codeception\TestCase\Test $this->assertSame('yyy', $this->uri->param('test')); } + public function testFragment() + { + $this->uri->initializeWithURL('http://localhost:8080/a/b/c#my-fragment'); + $this->assertSame('my-fragment', $this->uri->fragment()); + $this->uri->initializeWithURL('http://localhost:8080/a/b/c'); + $this->uri->fragment('something-new'); + $this->assertSame('something-new', $this->uri->fragment()); + } + public function testUrl() { $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper')->init();