diff --git a/src/Illuminate/Routing/RouteUrlGenerator.php b/src/Illuminate/Routing/RouteUrlGenerator.php index 7eb57eeb0e7a..768681d8ed81 100644 --- a/src/Illuminate/Routing/RouteUrlGenerator.php +++ b/src/Illuminate/Routing/RouteUrlGenerator.php @@ -98,7 +98,7 @@ public function to($route, $parameters = [], $absolute = false) if (! $absolute) { $uri = preg_replace('#^(//|[^/?])+#', '', $uri); - if ($base = $this->request->getBasePath()) { + if ($base = $this->request->getBaseUrl()) { $uri = preg_replace('#^'.$base.'#i', '', $uri); } diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index f96f1f9654c5..150f98d7d4cf 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -64,7 +64,7 @@ public function testBasicGenerationWithHostFormatting() $this->assertEquals('/named-route', $url->route('plain', [], false)); } - public function testBasicGenerationWithRequestBasePath() + public function testBasicGenerationWithRequestBaseUrlWithSubfolder() { $request = Request::create('http://www.foo.com/subfolder/foo/bar/subfolder/'); @@ -79,7 +79,47 @@ public function testBasicGenerationWithRequestBasePath() $route = new Route(['GET'], 'foo/bar/subfolder', ['as' => 'foobar']); $routes->add($route); + $this->assertEquals('/subfolder', $request->getBaseUrl()); + $this->assertEquals('/foo/bar/subfolder', $url->route('foobar', [], false)); + } + + public function testBasicGenerationWithRequestBaseUrlWithSubfolderAndFileSuffix() + { + $request = Request::create('http://www.foo.com/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( + $routes = new RouteCollection, + $request + ); + + $route = new Route(['GET'], 'foo/bar/subfolder', ['as' => 'foobar']); + $routes->add($route); + $this->assertEquals('/subfolder', $request->getBasePath()); + $this->assertEquals('/subfolder/index.php', $request->getBaseUrl()); + $this->assertEquals('/foo/bar/subfolder', $url->route('foobar', [], false)); + } + + public function testBasicGenerationWithRequestBaseUrlWithFileSuffix() + { + $request = Request::create('http://www.foo.com/other.php'); + + $request->server->set('SCRIPT_FILENAME', '/var/www/laravel-project/public/other.php'); + $request->server->set('PHP_SELF', '/other.php'); + + $url = new UrlGenerator( + $routes = new RouteCollection, + $request + ); + + $route = new Route(['GET'], 'foo/bar/subfolder', ['as' => 'foobar']); + $routes->add($route); + + $this->assertEquals('', $request->getBasePath()); + $this->assertEquals('/other.php', $request->getBaseUrl()); $this->assertEquals('/foo/bar/subfolder', $url->route('foobar', [], false)); }