4

Can't Download File as CSV

I'm trying to export some data as CSV & every time i try to export data i got this error - Call to undefined method Symfony\Component\HttpFoundation\StreamedResponse::header()

I have tried different possible ways but they aren't working

My Function (Controller File)

public function exportd(Request $request)
{
    $fileName = 'data.csv';
    $data = json_decode($request->data);

    $headers = array(
        "Content-type"        => "text/csv",
        "Content-Disposition" => "attachment; filename=$fileName",
        "Pragma"              => "no-cache",
        "Cache-Control"       => "must-revalidate, post-check=0, pre-check=0",
        "Expires"             => "0"
    );

    $columns = array('Name', 'Gender', 'experrience', 'location', 'Pref Location', 'Salary'
    ...
    );

    $callback = function() use($data, $columns) {
        $file = fopen('php://output', 'w');
        fputcsv($file, $columns);

        foreach ($data as $da) {
            $row['Name']  = $da->name;
            $row['Gender']  = $da->gender;
            $row['experrience']    = $task->experrience;
            $row['Pref Location']  = $task->pref_loc;
            $row['Salary']  = $task->salary;
            ...



                fputcsv($file, array($row['Name'], $row['Gender'],$row['experrience'], $row['location'], $row['Pref Location'], $row['Salary'] 
                ...
                   ));
            }

            fclose($file);
        };

    return Response()->stream($callback, 200, $headers);
}

My Middleware (AuthCheck)

public function handle(Request $request, Closure $next)
{

    if(!session()->has('LoggedUser') && ($request->path() !='auth/login' && $request->path() !='auth/register' )){
        return redirect('auth/login')->with('fail','You must be logged in');
    }

    if(session()->has('LoggedUser') && ($request->path() == 'auth/login' || $request->path() == 'auth/register' ) ){
        return back();
    }
    return $next($request)->header('Cache-Control','no-cache, no-store, max-age=0, must-revalidate')
                          ->header('Pragma','no-cache')
                          ->header('Expires','0');
}
newdevv2
  • 201
  • 3
  • 10
  • I suppose you want to add the headers only when `$next($request)` is an instance of `Illuminate\Http\Response` (Not sure that the right class though) – Clément Baconnier Apr 29 '22 at 16:30

1 Answers1

3

$next($request) is an instance of Symfony\Component\HttpFoundation\StreamedResponse in your scenario.

StreamedResponse does not support ->header(...). If you want to preserve your ->header(...) call, you must verify that the response is an instance of Illuminate\Http\Response


public function handle(Request $request, Closure $next)
{

    if(!session()->has('LoggedUser') && ($request->path() !='auth/login' && $request->path() !='auth/register' )){
        return redirect('auth/login')->with('fail','You must be logged in');
    }

    if(session()->has('LoggedUser') && ($request->path() == 'auth/login' || $request->path() == 'auth/register' ) ){
        return back();
    }

    $response = $next($request);

    if($response instanceof \Illuminate\Http\Response) {
        return $response->header('Cache-Control','no-cache, no-store, max-age=0, must-revalidate')
                          ->header('Pragma','no-cache')
                          ->header('Expires','0');
    }

    return $response;

}

Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55