0

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.

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

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, privacy policy and cookie policy

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