Skip to content

Commit 552f353

Browse files
authored
[10.x] Use fallback when previous URL is the same as the current (#46234)
* fallback when previous URL is the same as the current * do not check fallback
1 parent 3799f7f commit 552f353

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Illuminate/Routing/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function previous($fallback = false)
158158

159159
$url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();
160160

161-
if ($url) {
161+
if ($url && rtrim($url, '/') !== $this->request->fullUrl()) {
162162
return $url;
163163
} elseif ($fallback) {
164164
return $this->to($fallback);

tests/Routing/RoutingUrlGeneratorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,32 @@ public function testPreviousPath()
734734
$this->assertSame('/bar', $url->previousPath('/bar'));
735735
}
736736

737+
public function testWhenPreviousIsEqualToCurrent()
738+
{
739+
$url = new UrlGenerator(
740+
new RouteCollection,
741+
Request::create('http://www.foo.com/')
742+
);
743+
744+
$url->getRequest()->headers->set('referer', 'http://www.foo.com/');
745+
$this->assertSame('http://www.foo.com', $url->previous());
746+
$this->assertSame('http://www.foo.com/bar', $url->previous('/bar'));
747+
748+
$url->setRequest(Request::create('http://www.foo.com/bar'));
749+
750+
$url->getRequest()->headers->set('referer', 'http://www.foo.com/bar');
751+
$this->assertSame('http://www.foo.com', $url->previous());
752+
$this->assertSame('http://www.foo.com/bar', $url->previous('/bar'));
753+
$this->assertSame('http://www.foo.com/baz', $url->previous('/baz'));
754+
755+
$url->setRequest(Request::create('http://www.foo.com/bar?page=2'));
756+
757+
$url->getRequest()->headers->set('referer', 'http://www.foo.com/bar?page=2');
758+
$this->assertSame('http://www.foo.com', $url->previous());
759+
$this->assertSame('http://www.foo.com/bar', $url->previous('/bar'));
760+
$this->assertSame('http://www.foo.com/bar?page=2', $url->previous('/bar?page=2'));
761+
}
762+
737763
public function testRouteNotDefinedException()
738764
{
739765
$this->expectException(RouteNotFoundException::class);

0 commit comments

Comments
 (0)