Laravel 发送无缓存标头

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

Laravel sending no-cache header

cachinglaravellaravel-4

提问by Kristoffer Svanmark

I'm having trouble with Laravel 4. Somehow the header

我在使用 Laravel 4 时遇到问题。不知何故标题

Cache-Control: no-cache 

Is always being sent in the response on all pages within my site. I can't find where or how to change it.

总是在我网站内所有页面的响应中发送。我找不到在哪里或如何更改它。

Cache-Control: no-cache

缓存控制:无缓存

Since this is affecting all my controllers where I'm presenting a view with View::MakeI would really like a way to change this globally.

由于这会影响我在其中展示视图的所有控制器,因此我View::Make真的很想要一种方法来全局更改它。

采纳答案by rap-2-h

If you want to use Cache, you can change its behavior in your "Response" object (returned by a controller method in this example) :

如果你想使用缓存,你可以在你的“响应”对象中改变它的行为(在这个例子中由控制器方法返回):

public function myControllerMethod() {
    $response = Response::make('something');
    $response->setLastModified(new DateTime("now"));
    $response->setExpires(new DateTime("tomorrow"));
    return $response;
}

It works in my environnement, I hope it will help.

它适用于我的环境,我希望它会有所帮助。

EDIT:

编辑:

If you want to set it globally, you can try this (in app/start/directory):

如果你想全局设置它,你可以试试这个(在app/start/目录中):

App::after(function($request, $response) {
    $response->setLastModified(new DateTime("now"));
    $response->setExpires(new DateTime("tomorrow"));
});

回答by jec006

To help someone else trying to find the answer for Laravel 5.4, this would be:

为了帮助其他人试图找到 Laravel 5.4 的答案,这将是:

namespace App\Http\Controllers;
use DateTime;

class MyController extends Controller
{
  public function index()
  {
    return response('my content here')
              ->setLastModified(new DateTime("now"))
              ->setExpires(new DateTime("tomorrow"));
   }
}

See also: https://laravel.com/docs/5.4/responsesfor more information on getting different content (templates etc) into a response call.

另请参阅:https: //laravel.com/docs/5.4/responses,了解有关在响应调用中获取不同内容(模板等)的更多信息。