CakePHP 3.x - 如何为图像资产配置 Cache-Control header?

CakePHP 3.x - How to configure the Cache-Control header for image assets?

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

此时我网站的header是:

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

我试着写:

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

但是没有用。你能建议我正确的设置吗?

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

HTTP caching and CakePHP's \Cake\Cache\Cache class彼此无关,后者用于服务器端缓存应用数据,如模型模式、翻译、查询结果等

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

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

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