[5.x] Respect the current site when returning a View #10109
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue where the current site wasn't resolved correctly when returning a view from an action route. Consider the following scenario:
Example
I'm returning a
View
in a controller from an action route.When the
cascade()
is hydrated in theView
class, it will resolve theCascade::instance()
from the container, where it was bound withSite::current()
.cms/src/View/View.php
Lines 188 to 194 in 5cb5da6
cms/src/Providers/ViewServiceProvider.php
Lines 39 to 41 in 5cb5da6
At the time when the
Cascade
is bound in the container, the current site will be resolved from the current URL. Because we are on an action route, the resolved site will always be the default site.cms/src/Sites/Sites.php
Lines 72 to 77 in 5cb5da6
This leads to the
View
always being hydrated with data from the default site.In my scenario, I rely on localized content being returned from the action route. To solve this, I figured that I could set the current site before returning the view:
However, this won't work, as the
Cascade
was already bound with the site resolved from the current request beforeSite::setCurrent($site)
is called.To resolve this issue, we can explicitly set the current site using the
withSite()
method before theCascade
is hydrated in thecascade()
method of theView
class.