BinaryFileResponse in Laravel undefined
Solution 1
The problem is that you're calling ->header()
on a Response
object that doesn't have that function (the Symfony\Component\HttpFoundation\BinaryFileResponse
class). The ->header()
function is part of a trait that is used by Laravel's Response class, not the base Symfony Response.
Fortunately, you have access to the headers
property, so you can do this:
$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin' , '*');
$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
return $response;
Solution 2
You may want to exclude headers or set different headers for file
download requests by checking the header
method exists in the returned Closure
.
File download requests typically have the header
method omitted from Closure
.
public function handle($request, Closure $next)
{
$handle = $next($request);
if(method_exists($handle, 'header'))
{
$handle->header('Access-Control-Allow-Origin' , '*')
->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
}
return $handle;
}
If you need to set headers for a file
request (as other answer suggested) $handle->headers->set()
can be used in an else
condition:
public function handle($request, Closure $next)
{
$handle = $next($request);
if(method_exists($handle, 'header'))
{
// Standard HTTP request.
$handle->header('Access-Control-Allow-Origin' , '*');
return $handle;
}
// Download Request?
$handle->headers->set('Some-Other-Header' , 'value')
return $handle;
}
Sebastian Grebe
Updated on March 14, 2021Comments
-
Sebastian Grebe about 3 years
I have got the following problem: I want to return an Image on the route /getImage/{id} The function looks like this:
public function getImage($id){ $image = Image::find($id); return response()->download('/srv/www/example.com/api/public/images/'.$image->filename); }
When I do this it returns me this:
FatalErrorException in HandleCors.php line 18: Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()
I have got
use Response;
at the beginning of the controller. I dont think that the HandleCors.php is the problem but anyway:<?php namespace App\Http\Middleware; use Closure; use Illuminate\Contracts\Routing\Middleware; use Illuminate\Http\Response; class CORS implements Middleware { public function handle($request, Closure $next) { return $next($request)->header('Access-Control-Allow-Origin' , '*') ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE') ->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application'); } }
I actually dont know why this happens since it is exactly like it is described in the Laravel Docs. I have updated Laravel when I got the error but this did not fix it.
-
Sebastian Grebe about 9 yearsI changed it like you said and it really did help but now get "Trying to get property of non-object" error in the second line.
-
treeface about 9 years@Sebi55 eh my bad. Clearly
->set()
isn't chainable. -
Sebastian Grebe about 9 yearsYeah that was my idea too. Any other idea how to fix the middleware to work with the BinaryFileResponse?
-
Rohan over 8 yearsI don't understand your answer particularly but it worked for me. :)
-
manian about 7 years@treeface, It worked for me thanks, but it didn't work before adding semicolon to each statements. can you please add semicolon for each of the statements?. It will be helpful for future users
-
Adam Kozlowski almost 5 yearsDid you have any CORS problems with that solution?
-
Yohanim over 2 yearsdont forget
headers
notheader