Skip to content

Commit

Permalink
[5.6] remove full baseUrl from generated url paths (follow up) (#24101)
Browse files Browse the repository at this point in the history
* use getBaseUrl() instead of getBasePath()

* add tests for request url with filename suffix

* add test case for files not in a folder
  • Loading branch information
tomschlick authored and taylorotwell committed May 4, 2018
1 parent e1c990a commit 1fc6cd5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/RouteUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
42 changes: 41 additions & 1 deletion tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/');

Expand All @@ -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));
}

Expand Down

0 comments on commit 1fc6cd5

Please sign in to comment.