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]]);
  //$url = Url::fromUri($query); 
  return RedirectResponse::create($url->toString(), 302); 

  $form_state>setResponse(newTrustedRedirectResponse('https://google.com', 302)); 
  return new RedirectResponse($query); 
  $response = new RedirectResponse($query); 

tried above all methods, but nothing works.

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

1 Answer 1


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');

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .