在我的行动中,我是这样做的:
public function actionEvents(){
Yii::$app->response->headers->set('last-modified','2020-01-19');
return $this->modelClass::find()->all();
}
我也尝试过:
Yii::$app->response->headers->set('last-modified',(date_format('2020-01-19','Y/m/d H:i:s')));
但是我得到了 last-modifed
的条目,它根本不存在,但我得到的日期就像
最后修改时间:1970 年 1 月 1 日星期四 00:00:00 GMT
当我使用 2020-01-19
和 date_format
时,我收到 500 错误。
我该如何解决这个问题。
更新: 我可以看到缓存控制 header 是这样的:
缓存控制:无存储、无缓存、必须重新验证
这可能是原因,因为 last-modified
没有获得正确的日期。
我如何将缓存控制设置为 1 分钟。
相关操作代码:
public function actionEvents(){
$dateModified = '2020-01-19';
$date = new \DateTime($dateModified);
Yii::$app->response->headers->set('last-modified', date_format($date, 'Y/m/d H:i:s'));
return $this->modelClass::find()->all();
}
注意:相关 URL 操作是 API 端点。
最佳答案
您正在使用字符串,而您应该将 DateTimeInterface
作为 date_format
的第一个参数传递,否则会引发警告,请将其更改为
$dateModified = '2020-01-19';
$date = new DateTime($dateModified);
Yii::$app->response->headers->set('last-modified', date_format($date, 'Y/m/d H:i:s'));
关于php - Yii2 - 设置响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59805594/