【问题标题】:Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header() in RevalidateBackHistory.php在 RevalidateBackHistory.php 中调用未定义的方法 Symfony\Component\HttpFoundation\BinaryFileResponse::header()
【发布时间】: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


    【解决方案1】:

    您应该使用以下代码编辑您的 handle 函数。

    $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;
    

    用于在 RevalidateBackHistory 中间件文件中设置标题

    【讨论】:

    • 回报在哪里?
    • return $response; 结尾
    猜你喜欢
    • 1970-01-01
    • 2017-09-21
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2017-06-06
    • 1970-01-01
    相关资源
    最近更新 更多