Chloe
Chloe

Reputation: 26274

PHP Fatal Error – yii\base\ErrorException Access to undeclared static property: yii\filters\HttpCache::$cacheControlHeader

I'm trying to set this property, but it's failing.

Code (view):

        if (Yii::$app->user->identity && Yii::$app->user->identity->admin):
          \yii\filters\HttpCache::$cacheControlHeader = 'no-cache'; // prevent Fastly from caching admin links

Docs: http://www.yiiframework.com/doc-2.0/guide-caching-http.html#cache-control

Yii 2.0.7.

Upvotes: 0

Views: 1858

Answers (1)

M Sost
M Sost

Reputation: 1153

That property isn't a static property.

To set the cache control header, you'll need to add that class as a controller filter and set to property there. Something like this should work (YMMV with respect to extending controllers/behavior definitions):

public function behaviors() {
    return array(
        array(
            "class" => "yii\filters\HttpCache",
            "cacheControlHeader" => Yii::$app->user->identity && Yii::$app->user->identity->admin ? 
                "no-cache" : "public, max-age=3600",
        ),
    );
}

Upvotes: 1

Related Questions