laravel 如何使用中间件向响应添加标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28428451/
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 10:53:22  来源:igfitidea点击:

How do you add headers to a response with a middleware?

laravellaravel-5

提问by Marwelln

I can't figure out how to how to add headers to a response from a middleware. I've used both ->header(...)and ->headers->set(...)but both gives errors. So how do you do it?

我不知道如何向来自中间件的响应添加标头。我已经使用了两者->header(...)->headers->set(...)但都给出了错误。你是怎么做到的?

First I tried with

首先我试过

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

    $response->headers->set('refresh', '5;url=' . route('foo'));

    return $response;
}

which is the same as in Illuminate\Http\Middleware\FrameGuard.php, but that gives

与 in 相同Illuminate\Http\Middleware\FrameGuard.php,但这给出了

Call to a member function set() on a non-object

在非对象上调用成员函数 set()

Second I tried with

第二个我试过

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

    $response->header('refresh', '5;url=' . route('foo'));

    return $response;
}

But that gives

但这给

Method [header] does not exist on view.

视图中不存在方法 [header]。

So how do you add headers from a middleware?

那么如何从中间件添加标头呢?

采纳答案by Marwelln

I solved this by using the responsehelper.

我通过使用response助手解决了这个问题。

use Illuminate\Http\RedirectResponse;

$response = $next($request);
$response = $response instanceof RedirectResponse ? $response : response($response);

return $response->header('refresh', '5;url=' . route('foo'));

All my other middleware seems to run fine with this so I guess it's fine.

我的所有其他中间件似乎都运行良好,所以我想这很好。

回答by Waqas Bukhary

Here is a solution tested in Laravel 5.0to attach headers to routes

这是在Laravel 5.0 中测试的将标头附加到路由的解决方案

Create a middleware file app/Http/Middleware/API.php

创建中间件文件 app/Http/Middleware/API.php

<?php namespace App\Http\Middleware;
use Closure;
class API {

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

            $response = $next($request);
            $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Content-Range, Content-Disposition, Content-Description, X-Auth-Token');
            $response->header('Access-Control-Allow-Origin', '*');
            //add more headers here
            return $response;
        }
}

Add middlewear to kernel file by adding these lines to /app/Http/Kernel.php

通过将这些行添加到内核文件中,将中间件添加到 /app/Http/Kernel.php

protected $middleware = [
    //... some middleware here already 
    '\App\Http\Middleware\API',// <<< add this line if you wish to apply globally
];
protected $routeMiddleware = [
    //... some routeMiddleware here already 
    'api' => '\App\Http\Middleware\API', // <<< add this line if you wish to apply to your application only
];

Group your routes in the routes file /app/Http/routes.php

在路由文件中对路由进行分组 /app/Http/routes.php

Route::group(['middleware' => 'api'], function () {
    Route::get('api', 'ApiController@index');
    //other routes 
});