Ejemplo n.º 1
0
 public function actionView()
 {
     $id = \Yii::$app->request->get('id', 0);
     $alias = \Yii::$app->request->get('alias', 'non-alias');
     Post::updateAllCounters(['views' => 1], ['id' => $id]);
     $cache = \Yii::$app->getCache();
     $cache->keyPrefix = 'post-';
     $data = $cache->get('post_' . $id);
     if ($data === false) {
         $post = Post::findOne(['id' => $id, 'alias' => $alias]);
         if ($post) {
             $data = $post;
             $cache->set('post_' . $id, $data);
         } else {
             throw new NotFoundHttpException('Post not found');
         }
     }
     //var_dump($data);die();
     \Yii::$app->view->title = $data->title;
     return $this->render('view', ['post' => $data]);
 }