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.
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With