Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Routing] fix router base url when default uri has trailing slash #46097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2022

Conversation

Tobion
Copy link
Contributor

@Tobion Tobion commented Apr 18, 2022

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

When the router default uri (feature #36651) has a trailing slash, the generated URLs are wrong. E.g.

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

* The base URL never ends with a /.

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.

$this->assertSame('test.com', $requestContext->getHost());
$this->assertSame(8080, $requestContext->getHttpPort());
$this->assertSame(443, $requestContext->getHttpsPort());
$this->assertSame('', $requestContext->getBaseUrl());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the main fix. all others are just adding missing test cases.

@Tobion Tobion force-pushed the fix-base-url-trailing-slash branch from 5b9b708 to d60a6b8 Compare April 18, 2022 19:44
@Tobion Tobion force-pushed the fix-base-url-trailing-slash branch from d60a6b8 to 07136a9 Compare April 18, 2022 21:45
@nicolas-grekas
Copy link
Member

Shouldn't this target 4.4?

@Tobion
Copy link
Contributor Author

Tobion commented Apr 19, 2022

Well in Symfony the problem is only exposed since the default_uri config which was introduced in 5.x
The fromUri method does not exist in 4.4.

@nicolas-grekas
Copy link
Member

Thank you @Tobion.

@nicolas-grekas nicolas-grekas merged commit c14d0a1 into symfony:5.4 Apr 19, 2022
@Tobion Tobion deleted the fix-base-url-trailing-slash branch April 19, 2022 08:59
This was referenced Apr 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants