BinaryFileResponse in Laravel undefined BinaryFileResponse in Laravel undefined symfony symfony

BinaryFileResponse in Laravel undefined


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;


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;    }


If you have created middleware to prevent back history and now when you want to download a file, you are getting the following error: Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()

You should therefore edit yout PreventBackHistory file to:

public function handle($request, Closure $next)    {         $headers = [            'Cache-Control'      => 'nocache, no-store, max-age=0, must-revalidate',            'Pragma'     => 'no-cache',            'Expires' => 'Sun, 02 Jan 1990 00:00:00 GMT'        ];        $response = $next($request);        foreach($headers as $key => $value) {            $response->headers->set($key, $value);        }         return $response;           }

Inside your CORS.php file that you have created you should put the following lines of code:

 public function handle($request, Closure $next)    {        $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;     }