Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mprythero's avatar

Call to undefined method BinaryFileResponse::header()

I have an issue that appeared after updating the Maatwebsite Laravel Package but they are certain it isn't an issue on their package.

I get the following error: Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header() in my ModifyHeadersMiddleware.php file.

This comes up through the following controller function:

    public function summaryReport(){
        return Excel::download(new ClaimsExport, 'claims-report.xlsx');
    }

Under which my export is as such:

    <?php
    
    namespace App\Exports;
    
    use App\Claim;
    use Maatwebsite\Excel\Concerns\FromView;
    use Maatwebsite\Excel\Concerns\Exportable;
    use Illuminate\Contracts\View\View;
    
    class ClaimsExport implements FromView
    {
        use Exportable;
    
        /**
        * @return \Illuminate\Support\Collection
        */
        public function view(): View
        {
            return view('exports.osd.claims', [
                'claims' => Claim::where('type',2)->where('status',1)->whereNotNull('shipmentID')->orderBy('claimDate','desc')->get()
            ]);
        }
    }

And this is the section of code that is brought up in the ModifyHeadersMiddle.php file:

       public function handle($request, Closure $next)
    {
        $headers = [
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Methods'     => 'POST, GET, OPTIONS',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Max-Age'           => '86400',
            'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With'
        ];
 
        if ($request->isMethod('OPTIONS')) {
            return response()->json('{"method":"OPTIONS"}', 200, $headers);
        }
 
        $response = $next($request);
        foreach($headers as $key => $value) {
            $response->header($key, $value);
        }
 
        return $response;
    }

I'd appreciate any help anyone can give. Thank you so much!

0 likes
5 replies
nasircm's avatar

Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()

I already use Maatwebsite Laravel Package and work fine. but when i create middleware to prevent back history now when i want to download excel file then show me this error

this is my preventbackhistory middleware

public function handle($request, Closure $next)

{

    $response = $next($request);

    return $response->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')

        ->header('Pragma','no-cache')

        ->header('Expires','Sun, 02 Jan 1990 00:00:00 GMT');

}

any one can please help me

Kaps's avatar

Hi nasircm

You can use like. It is working for me and i hope this will help you.


$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;
1 like

Please or to participate in this conversation.