Response

Learn how to return a response that forces a file download

File Download

The \BitFrame\Message\ResponseTrait provides a handy withDownload() method to force a file download. The signature for the method is as follows:

$response->withDownload(  ): ResponseInterface;

For example:

$app->get("/about", function ($request, $response, $next) {
   // client gets 'file.css' as 'test.css'
   return $response->withDownload(
       'path/to/file.css',
       'test.css'
   );
});

Setting the second argument to a non-empty string in the withDownload() method would spoof the file name such that the client sees the file name you've supplied without actually renaming the actual file.

If the specified file cannot be found then a \BitFrame\Exception\FileNotReadableException is thrown.

This is a non-PSR standards based method, and is only available on class objects that use \BitFrame\Message\ResponseTrait. Default response objects created in BitFrame have this trait included automatically. The use of this helper method is strictly optional as it's merely meant as a shortcut to what you can just as easily accomplish without using it.

Comments

Let us know if you have something to say or add