From c9591b206782030ebd27c0d37321e8e1fd0c6e79 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 2 May 2018 10:57:24 -0400 Subject: [PATCH] [5.6] fix for route url generator when request()->basePath exists (#24074) * make sure Route::to() also removes the request base path * update implementation, add tests * update SCRIPT_FILENAME to avoid ambiguity --- src/Illuminate/Routing/RouteUrlGenerator.php | 8 +++++++- tests/Routing/RoutingUrlGeneratorTest.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/RouteUrlGenerator.php b/src/Illuminate/Routing/RouteUrlGenerator.php index cc849bb8e199..7eb57eeb0e7a 100644 --- a/src/Illuminate/Routing/RouteUrlGenerator.php +++ b/src/Illuminate/Routing/RouteUrlGenerator.php @@ -96,7 +96,13 @@ public function to($route, $parameters = [], $absolute = false) $uri = strtr(rawurlencode($uri), $this->dontEncode); if (! $absolute) { - return '/'.ltrim(preg_replace('#^(//|[^/?])+#', '', $uri), '/'); + $uri = preg_replace('#^(//|[^/?])+#', '', $uri); + + if ($base = $this->request->getBasePath()) { + $uri = preg_replace('#^'.$base.'#i', '', $uri); + } + + return '/'.ltrim($uri, '/'); } return $uri; diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index 587ec1c2b8a4..f96f1f9654c5 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -64,6 +64,25 @@ public function testBasicGenerationWithHostFormatting() $this->assertEquals('/named-route', $url->route('plain', [], false)); } + public function testBasicGenerationWithRequestBasePath() + { + $request = Request::create('http://www.foo.com/subfolder/foo/bar/subfolder/'); + + $request->server->set('SCRIPT_FILENAME', '/var/www/laravel-project/public/subfolder/index.php'); + $request->server->set('PHP_SELF', '/subfolder/index.php'); + + $url = new UrlGenerator( + $routes = new RouteCollection, + $request + ); + + $route = new Route(['GET'], 'foo/bar/subfolder', ['as' => 'foobar']); + $routes->add($route); + + $this->assertEquals('/subfolder', $request->getBasePath()); + $this->assertEquals('/foo/bar/subfolder', $url->route('foobar', [], false)); + } + public function testBasicGenerationWithPathFormatting() { $url = new UrlGenerator(