Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel/Lumen file response

I need to stream file content (such as images and other mime types) from a Lumen resource server to a Laravel client server. I know in Laravel I can use:

$headers = ['Content-Type' => 'image/png']; 
$path = storage_path('/mnt/somestorage/example.png')
return response()->file($path, $headers);

However, the file method is absent in Laravel\Lumen\Http\ResponseFactory.

Any suggestions are very welcome.

like image 754
iep Avatar asked Jan 23 '18 13:01

iep


People also ask

How do you redirect lumens?

Redirecting To Named RoutesWhen you call the redirect helper with no parameters, an instance of Laravel\Lumen\Http\Redirector is returned, allowing you to call any method on the Redirector instance.

How fast is Laravel Lumen?

To be precise, Lumen handles 100 requests per second. So, if speed is your requirement, then you know which framework to choose.

What is difference between Laravel and Lumen?

Differences between Laravel and Lumen: Laravel is an MVC based full-stack web application framework which supports a lot of third-party tools like Spatie, Entrust, Socialite etc and frameworks. Lumen is a micro framework, used to develop micro-services and API development in high speed and less time.

What is the use of Lumen in Laravel?

Lumen utilizes the Illuminate components that power the Laravel framework. As such, Lumen is built to painlessly upgrade directly to Laravel when needed; for example, when you discover that you need more features out of the box than what Lumen offers.


1 Answers

In Lumen you can use Symfony's BinaryFileResponse.

use Symfony\Component\HttpFoundation\BinaryFileResponse

$type = 'image/png';
$headers = ['Content-Type' => $type];
$path = '/path/to/you/your/file.png';

$response = new BinaryFileResponse($path, 200 , $headers);

return $response;

You can find the documentation here.

like image 199
iep Avatar answered Nov 09 '22 12:11

iep