0

This question already has an answer here:

In the submitForm of a custom form, I have this:

$sFileName = 'sites/default/files/_private/bulletins/' . $form_state->getValue('adherent')[1] . ".pdf";
$response = new BinaryFileResponse($sFileName);
$response->setContentDisposition(
  ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  'bulletin.pdf'
);
$form_state->setResponse($response);  

This triggers the download of a pdf document and it works.
Then, I try to execute:

$form_state->setRedirect('<front>');

and that is not working!
In FormState::setResponse documentation, I read « If a response is set, it will be used during processing and returned directly. The form will not be rebuilt or redirected ».
I guess it is why my redirection to doesn't work...

How can I do it anyway?

marked as duplicate by kiamlaluno Jun 8 at 1:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You cannot. One thing excludes the other one. What are you trying to achieve, exactly? Why would you allow users to download a file and, at same time, redirect them to a different page? – kiamlaluno Mar 13 at 16:28
  • You could eventually first redirect users to a new page that allows them to download a file, but the new page needs to know which file must be downloaded. I cannot see any reason to do that. – kiamlaluno Mar 13 at 16:30
  • The form is about renewing membership to an association. If the answer is Yes (I want to renew my membership), the download of a predefined document customized for the current user is triggered (with no more action from the user) and then, as there is no more to do on the form page, my idea was to redirect the user to the front page. Hope it is more clear... – gbmapo Mar 13 at 16:51
  • Finally, I decided to abandon the idea of redirecting to another page after the form submission. – gbmapo Mar 14 at 12:48
-2

short answer RedirectResponse;

use Symfony\Component\HttpFoundation\RedirectResponse;
$response = new RedirectResponse('/where-ever');
$response->send();

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