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

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


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?

  • 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 '19 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 '19 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 '19 at 16:51
  • Finally, I decided to abandon the idea of redirecting to another page after the form submission. – gbmapo Mar 14 '19 at 12:48

short answer RedirectResponse;

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

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