Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header() in RevalidateBackHistory.php

I have written a middleware so that the user can't go again in log in page after logging in. Which will redirect to the admin panel if already logged in.

Here is the code:

class RevalidateBackHistory
{
    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','Fri, 01 Jan 1990 00:00:00 GMT');
    }

}

i have called it inside a controller called NoticeController

 public function __construct()
        {
            $this->middleware('auth');
            $this->middleware('revalidate'); //this is the middleware
        }

I have also defined a function inside this controller to download a file and the code is

public function downloadFile($id)
    {
        $notice = new Notice();
        $data = $notice->where('id',$id)->first();

        if (file_exists(public_path().'/uploads/files/'.$data->file))
        {
            return response()->download(public_path().'/uploads/files/'.$data->file);
        }
        else
        {
            Session()->flash('message.notice',"File not found");
            return redirect('admin/notice/info');
        }

    }

The download function is perfect , i have used this function in another controller also. But the problem is occurring inside this controller when i call downloadFile() function it gives follwing exception.

(1/1) FatalThrowableError

Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header() in RevalidateBackHistory.php (line 20) at RevalidateBackHistory->handle(object(Request), object(Closure))in Pipeline.php (line 148)

If i remove the revalidate middleware from constructor the function works fine. what could be the solution of this problem?

like image 418
Sujon Chondro Shil Avatar asked Dec 24 '22 12:12

Sujon Chondro Shil


1 Answers

You should edit your handle function with the following code.

$response = $next($request);
$headers = [
    'Cache-Control' => 'nocache, no-store, max-age=0, must-revalidate',
    'Pragma','no-cache',
    'Expires','Fri, 01 Jan 1990 00:00:00 GMT',
];

foreach($headers as $key => $value) {
    $response->headers->set($key, $value);
}

return $response;

For setting the headers in your RevalidateBackHistory middleware file

like image 113
Surabhi Avatar answered Apr 10 '23 16:04

Surabhi