I've extended a FormBase
and I've defined the submitForm
method. In this method I want to redirect to an external URL (https://example.com/my/path?amount=33&gift=0&product_id=1)
$myurl = "https://example.com/my/path?amount=33&gift=0&product_id=1";
$redirect = Url::fromUri($myurl);
$form_state->setRedirectUrl($redirect);
And I get the error:
Redirects to external URLs are not allowed by default, use \Drupal\Core\Routing\TrustedRedirectResponse for it.
Ok, no problem, let's create one:
$myurl = "https://example.com/my/path?amount=33&gift=0&product_id=1";
$redirect = new TrustedRedirectResponse($myurl);
$form_state->setRedirectUrl($redirect);
Here's the error:
Argument 1 passed to Drupal\Core\Form\FormState::setRedirectUrl() must be an instance of Drupal\Core\Url
So it appears we've gone full circle. Is there a way that maybe I can convert the TrustedRedirectResponse
to a Url
object?
setResponse
instead ofsetRedirectUrl
will address this issue.