Skip to content

Commit f67e12e

Browse files
committed
fix. URI setPath empty
1 parent 895f1e6 commit f67e12e

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

system/HTTP/URI.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,15 @@ public function setPath(string $path)
710710
{
711711
$this->path = $this->filterPath($path);
712712

713-
$this->segments = explode('/', $this->path);
713+
$tempPath = trim($this->path, '/');
714+
if ($tempPath === '')
715+
{
716+
$this->segments = [];
717+
}
718+
else
719+
{
720+
$this->segments = explode('/', $tempPath);
721+
}
714722

715723
return $this;
716724
}
@@ -726,7 +734,16 @@ public function refreshPath()
726734
{
727735
$this->path = $this->filterPath(implode('/', $this->segments));
728736

729-
$this->segments = explode('/', $this->path);
737+
$tempPath = trim($this->path, '/');
738+
if ($tempPath === '')
739+
{
740+
$this->segments = [];
741+
}
742+
else
743+
{
744+
$this->segments = explode('/', $tempPath);
745+
}
746+
$this->segments = explode('/', trim($this->path, '/'));
730747

731748
return $this;
732749
}
@@ -960,7 +977,15 @@ protected function applyParts(array $parts)
960977
// Populate our segments array
961978
if (isset($parts['path']) && $parts['path'] !== '')
962979
{
963-
$this->segments = explode('/', trim($parts['path'], '/'));
980+
$path = trim($parts['path'], '/');
981+
if ($path === '')
982+
{
983+
$this->segments = [];
984+
}
985+
else
986+
{
987+
$this->segments = explode('/', $path);
988+
}
964989
}
965990
}
966991

tests/system/HTTP/URITest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,4 +848,12 @@ public function testZeroAsURIPath()
848848
$this->assertEquals('/0', $uri->getPath());
849849
}
850850

851+
public function testEmptyURIPath()
852+
{
853+
$url = 'http://example.com/';
854+
$uri = new URI($url);
855+
$this->assertEquals([], $uri->getSegments());
856+
$this->assertEquals(0, $uri->getTotalSegments());
857+
}
858+
851859
}

0 commit comments

Comments
 (0)