How to set the Expires HTTP header on my iii2 api module?

I initialize my Api module in my class init()using this

public function init()
{
    //parent::init();
    Yii::$app->request->parsers = ['application/json' => 'yii\web\JsonParser'];
    Yii::$app->request->enableCsrfValidation = false;
    Yii::$app->response->format = Response::FORMAT_JSON;
    $headers = Yii::$app->response->headers;
    $headers->set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60)));
    Yii::$app->user->enableSession = false;
    Yii::$app->user->loginUrl = null;
}

It still gives me

ExpiresThu, 19 Nov 1981 08:52:00 GMT

EDIT: I tried using the php method headers below and it works. Setting the application type in JSON also works only with direct access to the header.

header("Pragma: cache");
header("Content-Type: application/json");
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60)));

I tried to establish this in my action. I also thought about setting up the Response Expires component in my web configuration, but I don’t know how to enter the value. When I try to set the headers property, I am assigned a read-only error value. I need to set a cache to respond to my volleyball request on Android. How can I achieve this in my module or my application?

+4
1

, :

$headers->set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60)));
0

Source: https://habr.com/ru/post/1611410/


All Articles