Yii Framework Forum

Dynamic Page Caching


(jammi ) #1

Hi guys,

I would like to cache my dynamic pages from the database, from one method.
For example

  • www.domain.tld/cars/red
  • www.domain.tld/cars/blue
  • www.domain.tld/cars/green

URL-Structure
www.domain.tld/cars/KEYWORD

My code doesn’t work, because if I call red-cars, only this page is cached for 60 sec.
But my goal is, to cache every dynamic page.
What I’m doing wrong?

public function behaviors()
{
    return [

        'pageCache' => [
            'class'       => 'yii\filters\PageCache',
            'only'        => ['view'],
            'duration'    => 10,
            'dependency'  =>
            [
                'class'   => 'yii\caching\DbDependency',
                'sql'     => 'SELECT COUNT(*) FROM dict_phrases',
            ],
            'variations' =>
            [
                \Yii::$app->language,
            ]
        ],

        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['POST'],
            ],
        ],
    ];
}

(Francois Gannaz) #2

You forgot to show the declaration of the view action. I suppose it’s something like public function actionView($keyword) {.

The page cache you configured:

  • applies to the “view” page,
  • lasts for 10 seconds,
  • is refreshed every time the number of records inside the dict_phrases table changes,
  • stores a distinct page when the application language is different.

It seems that your page varies not only with the language, but also with the keyword received. So you probably need to add the line \Yii::$app->request->get('keyword') under variations.


(jammi ) #3

great thank you very much!!
This works!