我在哪里可以在 Laravel 中设置标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17548569/
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
Where can I set headers in laravel
提问by Trying Tobemyself
I want to set headers as array('Cache-Control'=>'no-cache, no-store, max-age=0, must-revalidate','Pragma'=>'no-cache','Expires'=>'Fri, 01 Jan 1990 00:00:00 GMT');
for all my views, currently I'm doing this in all controllers while returning views, like
我想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);`
So instead of writing this for each and every route can it be done in global scope, so that headers are set for every view.
因此,可以在全局范围内完成,而不是为每个路由编写此内容,以便为每个视图设置标头。
I tried setting headers by creating after filter, but didn't get it to work.
我尝试通过在过滤器后创建来设置标题,但没有让它工作。
Can anyone tell me where can I set the headers for all my views?
谁能告诉我在哪里可以设置所有视图的标题?
UPDATEOne of my view file meta content
更新我的视图文件元内容之一
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
Now when i use Redirect::to('/',301,$headers)
The header in firebug is
现在,当我使用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
And when I use Redirect::to('/');
当我使用 Redirect::to('/');
The header in firebug is
firebug 中的标题是
Cache-Control no-cache
Connection Keep-Alive
Content-Type text/html; charset=UTF-8
Date Tue, 09 Jul 2013 14:52:08 GMT
采纳答案by Laurence
There are a couple of different ways you could do this - all have advantages/disadvantages.
有几种不同的方法可以做到这一点 - 都有优点/缺点。
Option 1 (simple):Since the array is just static data - just manually put the headers in your view layouts directly - i.e. dont pass it from anywhere - code it straight in your view.
选项 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
?>
Option 2:Use view composers. You can use an App before filter to bind the header to all views in your app.
选项 2:使用视图作曲家。您可以在过滤器之前使用应用程序将标题绑定到应用程序中的所有视图。
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);
});
Then just echo out the $headers in your view(s).
然后在您的视图中回显 $headers 。
Note: you must let the view set your headers - that is why we are 'passing' the header into view for Laravel to handle. If you try and output the header itself from within a filter or something, you'll cause issues.
注意:你必须让视图设置你的标题 - 这就是为什么我们将标题“传递”到 Laravel 处理的视图中。如果您尝试从过滤器或其他东西中输出标头本身,则会导致问题。
Edit Option 3:I just found out about this - you could try this
编辑选项 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');
});
回答by Amarnasan
In Laravel 5, using Middleware, creating a new file, modifying an existing file:
在 Laravel 5 中,使用中间件,创建一个新文件,修改一个现有文件:
New file: app/Http/Middleware/AddHeaders.php
新文件: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;
}
}
Modify existing file app/Kernel.php
修改现有文件app/Kernel.php
protected $middleware = [
.
.
.
'App\Http\Middleware\AddHeaders',
];
And you're set.
你已经准备好了。
回答by Ben Bos
In Laravel 4, this works for me:
在 Laravel 4 中,这对我有用:
In filters.php:
在filters.php中:
App::after(function($request, $response)
{
$response->headers->set('key','value');
});
Like:
喜欢:
App::after(function($request, $response)
{
$response->headers->set('P3P','CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"');
});
回答by Marius Catalin
In Laravel 5 you can change /public/index.php line 55 and set your header for entire app:
在 Laravel 5 中,您可以更改 /public/index.php 第 55 行并为整个应用程序设置标题:
$response->send();
with:
和:
$response->header('Content-Type','text/html; charset=ISO-8859-1')->send();
for essample.
对于 essample。
回答by BlackPearl
For Laravel >= 5.2 yet, following @Amarnasan answer , although I used mine for API calls
对于 Laravel >= 5.2,遵循@Amarnasan answer ,尽管我使用我的 API 调用
In Laravel 5, using Middleware, creating a new file, modifying an existing file:
在 Laravel 5 中,使用中间件,创建一个新文件,修改一个现有文件:
New file: app/Http/Middleware/AddHeaders.php
新文件: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;
}
}
Modify existing file app/Kernel.php so that you can use with each specific route
修改现有文件 app/Kernel.php 以便您可以与每个特定路由一起使用
protected $routeMiddleware = [
.
.
.
'myHeader' => \App\Http\Middleware\AddHeaders::class,
];
And you're set.
Then you can use it like so for individual routes or groups
然后您可以像这样将其用于单个路线或团体
$api->get('cars/all', 'MyController@index')->middleware(['myHeader']);;
回答by MikeWu
Working on Laravel 4.2. I'm using filter for this, so in filters.php i have:
在 Laravel 4.2 上工作。我为此使用过滤器,所以在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
});
Than I attach this filter to routes or controllers. Controller attached looks like this for me:
比我将此过滤器附加到路由或控制器。附加的控制器对我来说是这样的:
public function __construct() {
$this->beforeFilter('onestep',array('except' => 'getLogin'));
$this->beforeFilter('csrf',array('on' => 'post'));
$this->afterFilter("no-cache", ["only"=>"getIndex"]);
}
This filter is attached as afterFilter.
此过滤器作为 afterFilter 附加。
回答by camelCase
For future readers using Laravel 5.x, this can be handled out of the box without needing to create any custom middleware.
对于未来使用 Laravel 5.x 的读者,这可以开箱即用,无需创建任何自定义中间件。
Laravel has the response()
helper method, which you can chain headers to very easily.
Laravel 有response()
helper 方法,你可以很容易地将头链接到它。
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'
]);
Read more about it in the documentation, because it can handle attaching a number of things; cookies
, views
, and more.