我编写了一个中间件,以便用户在登录后无法再次进入登录页面。如果已经登录,它将重定向到管理面板。
这是代码:
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');
}
}
我在一个名为NoticeController的控制器中调用了它
public function __construct()
{
$this->middleware('auth');
$this->middleware('revalidate'); //this is the middleware
}
我还在这个控制器中定义了一个函数来下载文件,代码是
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');
}
}
下载功能很完美,我在另一个控制器上也使用过这个功能。但当我调用 downloadFile() 时,问题出现在该控制器内部函数它给出以下异常。
(1/1) 致命抛出错误
调用未定义的方法 Symfony\Component\HttpFoundation\BinaryFileResponse::header()
在 RevalidateBackHistory.php 中(第 20 行)
在 Pipeline.php 中的 RevalidateBackHistory->handle(object(Request), object(Closure))(第 148 行)
如果我删除重新验证构造函数中的中间件该功能工作正常。这个问题的解决办法是什么?