我在哪里可以在laravel中设置标题 [英] Where can I set headers in 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');
,目前我正在所有控制器中执行此操作,同时返回视图,例如
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
$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.
我尝试通过创建after过滤器来设置标题,但没有使它起作用.
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?
更新 我的视图文件元内容之一
UPDATE One of my view file meta content
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
现在当我使用Redirect::to('/',301,$headers)
时
萤火虫的标头是
Now when i use Redirect::to('/',301,$headers)
The header in firebug is
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('/');
firebug中的标头是
The header in firebug is
Cache-Control no-cache
Connection Keep-Alive
Content-Type text/html; charset=UTF-8
Date Tue, 09 Jul 2013 14:52:08 GMT
推荐答案
您可以通过几种不同的方法来完成此操作-都有优点/缺点.
There are a couple of different ways you could do this - all have advantages/disadvantages.
选项1(简单): 由于数组只是静态数据-只需将标头直接手动放置在您的视图布局中-即不要从任何地方传递它-直接在视图中对其进行编码.
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.
<?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:使用查看作曲家.您可以在过滤器前使用应用程序,将标题绑定到应用程序中的所有视图.
Option 2: Use view composers. You can use an App before filter to bind the header to all views in your app.
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);
});
然后在您的视图中回显$ headers.
Then just echo out the $headers in your view(s).
注意:您必须让视图设置标头-这就是为什么我们将标头传递"到视图中以便Laravel处理.如果尝试从过滤器之类的东西中输出标头本身,则会引起问题.
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.
编辑选项3:我刚刚发现了这一点-您可以尝试
Edit Option 3: I just found out about this - you could try this
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中设置标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!