Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

undeportedmexican's avatar

Download or Render view in the same controller

Hello people!

So, maybe I'm going the wrong way here.

I'm building a reports module for my site, and one of the features that I want to include is to be able to download the data in a CSV file.

Now, I do know how to trigger downloads from Inertia, however, I don't want to maintain 2 different controllers, that will look essentially the same (the query would be the exact same one), just that for one I would render the Inertia View, and for the other one I want to send a CSV file (with the same data).

Is there any way to accomplish this? Am I off the track by trying this?

Thanks all!

0 likes
8 replies
Snapey's avatar
Snapey
Best Answer
Level 122

one controller, two methods, both calling the same function to prepare the data?

undeportedmexican's avatar

@Snapey I guess this makes sense. Keeping the data inside a separate method seems simple enough. And each method will do specifically what it's supposed to do.

Thanks for your answer!

undeportedmexican's avatar

@martinbean Because I was trying to use the same controller that normally would return an inertia view, which is triggered by an inertia request.

Since Inertia requests expect an Inertia response, when it's something else it displays it on a pop-up modal (like an error 500, for instance). So, at least I think, if I return an download response, on an inertia request, the same popup would appear, which is what I am trying to work around.

Sinnbeck's avatar

@undeportedmexican for downloads I just use a regular a element, instead of an inertia link

<a href="/path/to/download" download >download</a>
undeportedmexican's avatar

Thank you all guys ... in the end I settled with the creating 2 separate routes, and extracting the query logic. As @snapey initially suggested.

Please or to participate in this conversation.