我想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);`
因此,可以为全局视图设置标头,而不是为每条路由编写此代码,而可以在全局范围内完成。
我尝试通过在after过滤器后创建来设置标题,但没有使其正常工作。
谁能告诉我在哪里可以设置所有视图的标题?
更新我的视图文件元内容之一
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
现在当我使用Redirect::to('/',301,$headers)
firebug中的标题是
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:使用视图编写器。您可以在过滤器前使用应用程序,将标题绑定到应用程序中的所有视图。
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。
注意:您必须让视图设置标头-这就是我们将标头“传递”到视图中以便Laravel处理的原因。如果尝试从过滤器或其他内容中输出标头本身,则会导致问题。
编辑选项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');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句