Skip to content

Commit c14d0a1

Browse files
bug #46097 [Routing] fix router base url when default uri has trailing slash (Tobion)
This PR was merged into the 5.4 branch. Discussion ---------- [Routing] fix router base url when default uri has trailing slash | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | | License | MIT | Doc PR | When the router default uri (feature #36651) has a trailing slash, the generated URLs are wrong. E.g. ```yaml framework: router: default_uri: 'https://example.org/' # this is equivalent URI to 'https://example.org' ``` Generating any absolute URL given this base path currently resulted in double slashes, e.g. `https://example.org//mypage` because the base url is set to `/` and the path info defaults to `/` as well. This is not correct and will result in a 404. The most consistent fix with the rest of symfony is to always rtrim the trailing slashes from the base url. This is already done in the HttpFoundation Request class see https://github.com/symfony/symfony/blob/674ad07a684fe1274ae49d9f4b6294ede3b47b92/src/Symfony/Component/HttpFoundation/Request.php#L849 So I think it makes sense to enforce this also on the requestcontext so it is also the case when it does not go through the fromRequest but via fromUri in the CLI. Commits ------- 07136a9 [Routing] fix router base url when default uri has trailing slash
2 parents a9b9bd1 + 07136a9 commit c14d0a1

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Symfony/Component/Routing/RequestContext.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function getBaseUrl()
9898
*/
9999
public function setBaseUrl(string $baseUrl)
100100
{
101-
$this->baseUrl = $baseUrl;
101+
$this->baseUrl = rtrim($baseUrl, '/');
102102

103103
return $this;
104104
}

src/Symfony/Component/Routing/Tests/RequestContextTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,51 @@ public function testConstruct()
4040
$this->assertEquals('bar=foobar', $requestContext->getQueryString());
4141
}
4242

43+
public function testFromUriWithBaseUrl()
44+
{
45+
$requestContext = RequestContext::fromUri('https://test.com:444/index.php');
46+
47+
$this->assertSame('GET', $requestContext->getMethod());
48+
$this->assertSame('https', $requestContext->getScheme());
49+
$this->assertSame('test.com', $requestContext->getHost());
50+
$this->assertSame('/index.php', $requestContext->getBaseUrl());
51+
$this->assertSame('/', $requestContext->getPathInfo());
52+
$this->assertSame(80, $requestContext->getHttpPort());
53+
$this->assertSame(444, $requestContext->getHttpsPort());
54+
}
55+
56+
public function testFromUriWithTrailingSlash()
57+
{
58+
$requestContext = RequestContext::fromUri('http://test.com:8080/');
59+
60+
$this->assertSame('http', $requestContext->getScheme());
61+
$this->assertSame('test.com', $requestContext->getHost());
62+
$this->assertSame(8080, $requestContext->getHttpPort());
63+
$this->assertSame(443, $requestContext->getHttpsPort());
64+
$this->assertSame('', $requestContext->getBaseUrl());
65+
$this->assertSame('/', $requestContext->getPathInfo());
66+
}
67+
68+
public function testFromUriWithoutTrailingSlash()
69+
{
70+
$requestContext = RequestContext::fromUri('https://test.com');
71+
72+
$this->assertSame('https', $requestContext->getScheme());
73+
$this->assertSame('test.com', $requestContext->getHost());
74+
$this->assertSame('', $requestContext->getBaseUrl());
75+
$this->assertSame('/', $requestContext->getPathInfo());
76+
}
77+
78+
public function testFromUriBeingEmpty()
79+
{
80+
$requestContext = RequestContext::fromUri('');
81+
82+
$this->assertSame('http', $requestContext->getScheme());
83+
$this->assertSame('localhost', $requestContext->getHost());
84+
$this->assertSame('', $requestContext->getBaseUrl());
85+
$this->assertSame('/', $requestContext->getPathInfo());
86+
}
87+
4388
public function testFromRequest()
4489
{
4590
$request = Request::create('https://test.com:444/foo?bar=baz');

0 commit comments

Comments
 (0)