我已經實現了對服務器或其他受保護圖像的媒體視圖。控制器處理請求並檢查權限,並相應地用$this->response->file
來服務器文件。在CakePhp中緩存使用response-> file()發送的圖像
但是,我還沒有設法獲取圖像緩存,迫使瀏覽器總是重新下載(我知道這是從Firebug的網絡面板)。這些文件不太可能發生變化,因此緩存會大大加快速度。
我玩過$this->response->modified()
和cache()
,但無濟於事。
我該如何在控制器操作中實現一個檢查來服務器文件還是不修改狀態碼?還是我在這裏?
任何幫助表示讚賞! :)
public function view($id)
{
$this->loadModel('Image');
$res = $this->Image->hasAccess($id, $this->Auth->user('subject_id'));
if($res['access'] || $this->isAdmin()){
$modified = gmdate("D, j M Y G:i:s ", filemtime (APP . "Content/Images/" . $res['file']));
$this->response->modified($modified);
$this->response->expires(time() + 60 * 60 * 24 * 31);
$this->response->file(APP . "Content/Images/" . $res['file']);
}else if($res['access'] === false){
$this->response->statusCode(403);
}else if($res['access'] === null){
$this->response->statusCode(404);
}
return $this->response;
}
確切!我自己也想到了這一點。謝謝! :) –