Craft's Web Controller has a redirectToPostedUrl method. Copied here for the sake of discussion:
public function redirectToPostedUrl($object = null, string $default = null): YiiResponse
{
$url = Craft::$app->getRequest()->getValidatedBodyParam('redirect');
if ($url === null) {
if ($default !== null) {
$url = $default;
} else {
$url = Craft::$app->getRequest()->getPathInfo();
}
}
if ($object) {
$url = Craft::$app->getView()->renderObjectTemplate($url, $object);
}
return $this->redirect($url);
}
We can call this method from our plugin controllers to redirect to the posted URL in the redirect
input field and use a dynamic {siteUrl}
variable. In my template, I have a redirect value that uses {siteUrl}
:
<input type="hidden" name="redirect" value="6026760a62b9c6007a4b5695a65acbabG2540f8cb3727b998a1c78da45db863f{siteUrl}?id={id}">
And in my Controller, I receive that value and redirect to that location:
return $this->redirectToPostedUrl($object);
That all works fine. I can pass my custom $object
to this method and the {id}
or any other part of the model I wish to process can appear in the URL.
My question is, where is {siteUrl}
being made available to the redirectToPostedUrl
method? In looking closer at renderObjectTemplate
it never appears to specifically make global variables available. And, if I try other global variables such as {currentUser.id}
nothing gets rendered. In Craft 2 I believe this method made environmentalVariables
available for parsing but those are no longer supported in Craft 3.
So why and how does {siteUrl}
work? Are there other variables that are available to be used in the redirect URLs too that we don't explicitly make available by passing them in via the $object
argument?