Skip to content

Commit

Permalink
[5.6] fix for route url generator when request()->basePath exists (#2…
Browse files Browse the repository at this point in the history
…4074)

* make sure Route::to() also removes the request base path

* update implementation, add tests

* update SCRIPT_FILENAME to avoid ambiguity
  • Loading branch information
tomschlick authored and taylorotwell committed May 2, 2018
1 parent 1bf54d2 commit c9591b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Routing/RouteUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c9591b2

Please sign in to comment.