Laravel 中的 BinaryFileResponse 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29289177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:13:53  来源:igfitidea点击:

BinaryFileResponse in Laravel undefined

phphttpsymfonylaravel

提问by Sebi55

I have got the following problem: I want to return an Image on the route /getImage/{id} The function looks like this:

我遇到了以下问题:我想在路由上返回一个图像 /getImage/{id} 函数如下所示:

public function getImage($id){
   $image = Image::find($id);
   return response()->download('/srv/www/example.com/api/public/images/'.$image->filename);
}

When I do this it returns me this:

当我这样做时,它会返回给我:

FatalErrorException in HandleCors.php line 18:
Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()

I have got use Response;at the beginning of the controller. I dont think that the HandleCors.php is the problem but anyway:

我有use Response;控制器的开头。我不认为 HandleCors.php 是问题,但无论如何:

<?php namespace App\Http\Middleware;
use Closure;

use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Http\Response;

class CORS implements Middleware {

public function handle($request, Closure $next)
{
      return $next($request)->header('Access-Control-Allow-Origin' , '*')
            ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
            ->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
     }
}

I actually dont know why this happens since it is exactly like it is described in the Laravel Docs. I have updated Laravel when I got the error but this did not fix it.

我实际上不知道为什么会发生这种情况,因为它与 Laravel Docs 中描述的完全一样。当我收到错误时,我已经更新了 Laravel,但这并没有解决它。

回答by treeface

The problem is that you're calling ->header()on a Responseobject that doesn't have that function (the Symfony\Component\HttpFoundation\BinaryFileResponseclass). The ->header()function is part of a traitthat is used by Laravel's Response class, not the base Symfony Response.

问题是您正在调用->header()一个Response没有该函数(Symfony\Component\HttpFoundation\BinaryFileResponse类)的对象。该->header()函数是Laravel 的Response 类使用的特征的一部分,而不是基本的 Symfony Response。

Fortunately, you have access to the headersproperty, so you can do this:

幸运的是,您可以访问该headers属性,因此您可以执行以下操作:

$response = $next($request);

$response->headers->set('Access-Control-Allow-Origin' , '*');
$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');

return $response;

回答by Marc

You may want to exclude headers or set different headers for filedownload requests by checking the headermethod exists in the returned Closure.

您可能希望file通过检查header返回的Closure.

File download requests typically have the headermethod omitted from Closure.

文件下载请求通常会从 中header省略该方法Closure


public function handle($request, Closure $next)
{
    $handle = $next($request);

    if(method_exists($handle, 'header'))
    {
        $handle->header('Access-Control-Allow-Origin' , '*')
               ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
               ->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
    }

    return $handle;

}

If you need to set headers for a filerequest (as other answer suggested) $handle->headers->set()can be used in an elsecondition:

如果您需要为file请求设置标头(如其他答案所建议的那样)$handle->headers->set()可以在else条件中使用:


public function handle($request, Closure $next)
{
    $handle = $next($request);

    if(method_exists($handle, 'header'))
    {
        // Standard HTTP request.

        $handle->header('Access-Control-Allow-Origin' , '*');
    }
    else
    {
        // Download Request?

        $handle->headers->set('Some-Other-Header' , 'value')
    }

    return $handle;

}