4

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?

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

2 Comments

where is return?
return $response; at the end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.