1

Want to redirect $form_state to external URL after successful registration of the user. Like, need to redirect to facebook after user registration.

  $query = "http://facebook.com";
  $url = Url::fromRoute('user.register', [], ['query' => ['service' => $query]]);
  $form_state->setRedirectUrl($url); 
  //$url = Url::fromUri($query); 
  return RedirectResponse::create($url->toString(), 302); 

  $form_state>setResponse(newTrustedRedirectResponse('https://google.com', 302)); 
  return new RedirectResponse($query); 
  $form_state->setRedirect($query); 
  $form_state->setRedirectUrl(Url::fromUri($query));
  $response = new RedirectResponse($query); 
  $response->send();

tried above all methods, but nothing works.

1
  • You should use setRedirectUrl and Url::fromUri('http://facebook.com', ['absolute' => TRUE]); per the API docs for Url::fromUri.
    – mradcliffe
    Commented Oct 24, 2016 at 13:38

1 Answer 1

8

The method that you missed is $form_state->setResponse(). In combination with a TrustedRedirectResponse object, which exists for security reasons (to prevent users from accidentally being redirected to another site), you can use code similar to the following.

$response = new TrustedRedirectResponse('https://facebook.com');
$form_state->setResponse($response);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.