public function actionView($slug) { $model = new CategoryPost(); $category = $model->findOne(['slug' => $slug]); if (empty($category)) { throw new NotFoundHttpException('Không tìm thấy nội dung theo yêu cầu.'); } $posts = Post::find()->where(['category_id' => $category->id, 'published' => 10]); $count = $posts->count(); $pagination = new Pagination(['totalCount' => $count]); $nodes = $posts->offset($pagination->offset)->limit(10)->orderBy(['id' => SORT_DESC])->all(); return $this->render('view', ['category_title' => $category->title, 'nodes' => $nodes, 'pagination' => $pagination]); }
/** * Finds the CategoryPost model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return CategoryPost the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = CategoryPost::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }