From 17cd67bbc34699d9fa22937ba2353bfaec1bb24b Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Tue, 1 May 2018 20:34:25 -0400 Subject: [PATCH 1/3] make sure Route::to() also removes the request base path --- src/Illuminate/Routing/RouteUrlGenerator.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/RouteUrlGenerator.php b/src/Illuminate/Routing/RouteUrlGenerator.php index cc849bb8e199..2f65a54bcc00 100644 --- a/src/Illuminate/Routing/RouteUrlGenerator.php +++ b/src/Illuminate/Routing/RouteUrlGenerator.php @@ -96,7 +96,9 @@ public function to($route, $parameters = [], $absolute = false) $uri = strtr(rawurlencode($uri), $this->dontEncode); if (! $absolute) { - return '/'.ltrim(preg_replace('#^(//|[^/?])+#', '', $uri), '/'); + $uri = ltrim(preg_replace('#^(//|[^/?])+#', '', $uri), $this->request->getBasePath()); + + return '/'.ltrim($uri, '/'); } return $uri; From f46ec85681a997cfa91adb7182db84269a0b9ce7 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Tue, 1 May 2018 22:05:59 -0400 Subject: [PATCH 2/3] update implementation, add tests --- src/Illuminate/Routing/RouteUrlGenerator.php | 6 +++++- tests/Routing/RoutingUrlGeneratorTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/RouteUrlGenerator.php b/src/Illuminate/Routing/RouteUrlGenerator.php index 2f65a54bcc00..7eb57eeb0e7a 100644 --- a/src/Illuminate/Routing/RouteUrlGenerator.php +++ b/src/Illuminate/Routing/RouteUrlGenerator.php @@ -96,7 +96,11 @@ public function to($route, $parameters = [], $absolute = false) $uri = strtr(rawurlencode($uri), $this->dontEncode); if (! $absolute) { - $uri = ltrim(preg_replace('#^(//|[^/?])+#', '', $uri), $this->request->getBasePath()); + $uri = preg_replace('#^(//|[^/?])+#', '', $uri); + + if ($base = $this->request->getBasePath()) { + $uri = preg_replace('#^'.$base.'#i', '', $uri); + } return '/'.ltrim($uri, '/'); } diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index 587ec1c2b8a4..b32fe4613574 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/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( From da197f70610f9dba3343a27c30ab902ea54206de Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 2 May 2018 00:25:31 -0400 Subject: [PATCH 3/3] update SCRIPT_FILENAME to avoid ambiguity --- tests/Routing/RoutingUrlGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index b32fe4613574..f96f1f9654c5 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -68,7 +68,7 @@ public function testBasicGenerationWithRequestBasePath() { $request = Request::create('http://www.foo.com/subfolder/foo/bar/subfolder/'); - $request->server->set('SCRIPT_FILENAME', '/var/www/subfolder/index.php'); + $request->server->set('SCRIPT_FILENAME', '/var/www/laravel-project/public/subfolder/index.php'); $request->server->set('PHP_SELF', '/subfolder/index.php'); $url = new UrlGenerator(