Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc14d0a1

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.```yamlframework: 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 seehttps://github.com/symfony/symfony/blob/674ad07a684fe1274ae49d9f4b6294ede3b47b92/src/Symfony/Component/HttpFoundation/Request.php#L849So 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 parentsa9b9bd1 +07136a9 commitc14d0a1

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

‎src/Symfony/Component/Routing/RequestContext.php‎

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

103103
return$this;
104104
}

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

Lines changed: 45 additions & 0 deletions
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+
publicfunctiontestFromUriWithBaseUrl()
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+
publicfunctiontestFromUriWithTrailingSlash()
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+
publicfunctiontestFromUriWithoutTrailingSlash()
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+
publicfunctiontestFromUriBeingEmpty()
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
publicfunctiontestFromRequest()
4489
{
4590
$request = Request::create('https://test.com:444/foo?bar=baz');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp