lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.52k stars 124 forks source link

How to nicely implement excelReponse, csvResponse or pdfResponse other than htmlResponse/jsonResponse? #246

Closed xluk9 closed 1 month ago

xluk9 commented 1 year ago

Hello, nice project. Finally, I don't have to wander around files to get to the business logic. So, in my application I usually respond with HTML, JSON and sometime I also need to return an Excel file or PDF file. What would be the right approach here other than implementing if/else in the controller? Is there a convenient way to provide an array like

protected $responseTypes =  [
   "expectPDF" => "pdfResponse"
    ...
];

where the key (in this case is expectPDF) is the method to call on $response like $response->expectPDF(). If that call return true, then invoke pdfResponse on the Action class. Obviously the user should implement somewhere expectPDF, either as macro (don't know if doable) or in the Action class itself.

edalzell commented 1 year ago

You can return whatever you like from the handle method.

Wulfheart commented 1 month ago

Closing due to inactivity. If you feel your issue is still relevant please open a new one with a link to a repository containing a minimal reproducible example.