【问题标题】:CakePHP 3.x - How to configure the Cache-Control header for image assets?CakePHP 3.x - 如何为图像资产配置 Cache-Control 标头?
【发布时间】:2020-04-09 10:40:30
【问题描述】:

如何在我的 CakePHP 3.x 中打开缓存设置。理想情况下,我希望只为图像打开缓存(不适用于 CSS 或 JS 文件)

此时我网站的标题是:

Cache-Control: no-store, no-cache, must-revalidate

我试着写:

   Cache::enable(); in initialize() function of my AppController 

但它不起作用。你能建议我正确的设置吗?

EDIT 调用 if(Cache::enabled()) 结果为真 - 为什么我有上述标题是这种情况?

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    HTTP caching 和 CakePHP 的 \Cake\Cache\Cache class 没有任何关系,后者用于应用程序数据的服务器端缓存,例如模型模式、翻译、查询结果等。

    除非您的资产确实存在于插件中并且您没有 symlinked/copied them into your application's webroot folder(您应该这样做),否则资产由网络服务器提供,而不涉及 (Cake)PHP,即您必须相应地配置您的服务器。请参阅您正在使用的服务器软件的文档,或者可以在 StackOverflow 上找到有关此主题的许多问题。

    Apache 的一个小例子,在.htaccess 文件中使用Header directive

    <FilesMatch "\.(jpg|jpeg|png|gif)$">
        Header set Cache-Control "max-age=2592000, public"
        # ...
    </FilesMatch>
    

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多