/** * 获取文章列表(可以按分类、标签获取文章列表) * @return string */ public function actionIndex() { $category_id = Html::encode(Yii::$app->request->get('category_id')); $tag_id = Html::encode(Yii::$app->request->get('tag_id')); $data = Article::find()->where(['status' => Article::STATUS_DISPLAY, 'category_id' => $category_id]); if ($tag_id) { $article_tags = ArticleTag::find()->where(['tag_id' => $tag_id])->all(); $article_ids = ArrayHelper::getColumn($article_tags, 'article_id'); $data = $data->andWhere(['in', 'id', $article_ids]); } $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 20]); //全部文章 $articles = $data->orderBy(['publish_at' => SORT_DESC])->offset($pages->offset)->limit($pages->limit)->all(); //推荐文章 $recommendArticles = Article::getRecommendArticle(); //分类 $categories = Category::find()->where(['status' => Category::STATUS_DISPLAY])->all(); //热门标签 $tags = Tag::find()->orderBy(['article_count' => SORT_DESC, 'id' => SORT_DESC])->limit(10)->all(); return $this->render('index', ['articles' => $articles, 'recommendArticles' => $recommendArticles, 'categories' => $categories, 'tags' => $tags, 'category_id' => $category_id, 'tag_id' => $tag_id]); }