【发布时间】:2020-04-20 23:39:55
【问题描述】:
我写了一个中间件,这样用户在登录后就不能再次进入登录页面。如果已经登录,它将重定向到管理面板。
代码如下:
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) FatalThrowableError
调用未定义的方法 Symfony\Component\HttpFoundation\BinaryFileResponse::header() 在 RevalidateBackHistory.php 中(第 20 行) 在 RevalidateBackHistory->handle(object(Request), object(Closure)) in Pipeline.php(第 148 行)
如果我从构造函数中删除 revalidate 中间件,则该函数可以正常工作。这个问题的解决方案是什么?
【问题讨论】:
标签: php laravel-5.4