我在哪里可以在laravel中设置标题

我想设置头为array('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT'); 对于我所有的观点,目前我在所有控制器中都是这样做的,同时返回视图

 $headers=array('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT'); Redirect::to('/',301,$headers);` 

因此,不要为每个path都写这个,而应该在全局范围内完成,以便为每个视图设置标题。

我试图设置filter后创build标题,但没有得到它的工作。

任何人都可以告诉我在哪里可以设置所有我的意见标题?

更新我的观点文件元内容之一

 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 

现在,当我使用Redirect::to('/',301,$headers)在萤火虫头

 Cache-Control max-age=0, must-revalidate, no-cache, no-store, private Connection Keep-Alive Content-Type text/html; charset=UTF-8 Date Tue, 09 Jul 2013 14:52:08 GMT Expires Fri, 01 Jan 1990 00:00:00 GMT 

而当我使用Redirect::to('/');

萤火虫的标题是

 Cache-Control no-cache Connection Keep-Alive Content-Type text/html; charset=UTF-8 Date Tue, 09 Jul 2013 14:52:08 GMT 

有几种不同的方式可以做到这一点 – 都有优点/缺点。

选项1(简单):由于数组只是静态数据 – 只需手动将标题直接放在视图布局中即不要从任何地方传递 – 在视图中直接编码。

 <?php //set headers to NOT cache a page header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1 header("Pragma: no-cache"); //HTTP 1.0 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past ?> 

选项2:使用视图composer php 。 您可以在过滤之前使用应用程序将标题绑定到应用程序中的所有视图。

 App::before(function($request) { $headers=array('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT'); View::share('headers', $headers); }); 

然后只是在你的视图中回声$头。

注意:您必须让视图设置您的标题 – 这就是为什么我们将标题“传递”到Laravel处理的视图中。 如果您尝试从filter或其他内容中输出标题本身,则会导致问题。

编辑选项3:我刚刚发现了这个 – 你可以试试这个

 App::before(function($request) { Response::header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate'); Response::header('Pragma', 'no-cache'); Response::header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT'); }); 

在Laravel 4中,这适用于我:

在filters.php中:

 App::after(function($request, $response) { $response->headers->set('key','value'); }); 

喜欢:

 App::after(function($request, $response) { $response->headers->set('P3P','CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"'); }); 

在Laravel 5中,使用中间件创build一个新文件,修改现有的文件:

新文件: app / Http / Middleware / AddHeaders.php

 <?php namespace App\Http\Middleware; use Closure; use Illuminate\Contracts\Routing\Middleware; // If Laravel >= 5.2 then delete 'use' and 'implements' of deprecated Middleware interface. class AddHeaders implements Middleware { public function handle($request, Closure $next) { $response = $next($request); $response->header('header name', 'header value'); $response->header('another header', 'another value'); return $response; } } 

修改现有的文件app / Kernel.php

 protected $middleware = [ . . . 'App\Http\Middleware\AddHeaders', ]; 

你就定了

在Laravel上工作4.2。 我正在使用filter,所以在filters.php我有:

 Route::filter('no-cache',function($route, $request, $response){ $response->header("Cache-Control","no-cache,no-store, must-revalidate"); $response->header("Pragma", "no-cache"); //HTTP 1.0 $response->header("Expires"," Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past }); 

比我把这个filter附加到路由或控制器。 控制器连接看起来像这样对我来说:

 public function __construct() { $this->beforeFilter('onestep',array('except' => 'getLogin')); $this->beforeFilter('csrf',array('on' => 'post')); $this->afterFilter("no-cache", ["only"=>"getIndex"]); } 

此filter作为afterFilter附加。

在Laravel 5中,您可以更改/public/index.php第55行,并为整个应用程序设置标题:

 $response->send(); 

有:

 $response->header('Content-Type','text/html; charset=ISO-8859-1')->send(); 

为例。

对于Laravel> = 5.2,在@Amarnasan答案后,尽pipe我用我的API调用

在Laravel 5中,使用中间件创build一个新文件,修改现有的文件:

新文件:app / Http / Middleware / AddHeaders.php

 <?php namespace App\Http\Middleware; use Closure; use Illuminate\Routing\Redirector; use Illuminate\Http\Request; use Illuminate\Foundation\Applicaion; class AddHeaders { public function handle($request, Closure $next) { $response = $next($request); $response->header('Cache-Control', 'max-age=36000, public'); //$response->header('another header', 'another value'); return $response; } } 

修改现有的文件app / Kernel.php,以便您可以使用每个特定的路线

 protected $routeMiddleware = [ . . . 'myHeader' => \App\Http\Middleware\AddHeaders::class, ]; And you're set. 

那么你可以像个人路线或团体一样使用它

 $api->get('cars/all', 'MyController@index')->middleware(['myHeader']);; 

对于将来使用Laravel 5.x的读者,可以在不需要创build任何自定义中间件的情况下进行处理。

Laravel有response()辅助方法,您可以非常容易地链接标题。

 use Response; // Or possibly: use Illuminate\Http\Response; depending on your aliases used. // Add a series of headers return response($content) ->header('Content-Type', 'text/xml') ->header('X-Header-One', 'Header Value'); // Or use withHeaders to pass array of headers to be added return response($content) ->withHeaders([ 'Content-Type' => 'text/xml', 'X-Header-One' => 'Header Value' ]); 

在文档中阅读更多关于它的内容 ,因为它可以处理附加的许多事情; cookiesviews ,等等。