/** * 具体文章显示 * @return string */ public function actionArticle() { $categoryName = Yii::$app->request->get('dir'); $articleName = Yii::$app->request->get('name'); $categoryModel = new Category(); $articleInfo = $categoryModel->getArticleBody($categoryName, $articleName); $parser = new GithubMarkdown(); $articleInfo['content'] = $parser->parse($articleInfo['content']); $articleInfo['category'] = $categoryName; return $this->render('article', ['article' => $articleInfo]); }
/** * 获取首页需要展示的数据 * @param int $limit 首页默认展示条数,不传值显示所有 * @return array */ public function getIndexData($limit = null) { $allArticle = array(); $categoryList = self::getCategoryList(); //获取当前所有文章信息 $i = 0; foreach ($categoryList as $category) { //获取该分类下所有文章 $tmpArr = self::getArticleList($category['categoryName']); foreach ($tmpArr as $article) { $allArticle[$i] = $article; $i++; } } //按发表时间逆序得到最近的十篇文章 usort($allArticle, function ($a, $b) { $al = $a['postDate']; $bl = $b['postDate']; if ($al == $bl) { return 0; } return $al > $bl ? -1 : 1; }); $allArticle = array_slice($allArticle, 0, $limit); //获取文章具体内容以及把.md转成HTML $parser = new GithubMarkdown(); $returnArr['articles'] = array(); foreach ($allArticle as $key => $article) { $articleInfo = self::getArticleBody($article['category'], $article['articleName']); $returnArr['articles'][$key]['id'] = $articleInfo['id']; $returnArr['articles'][$key]['category'] = $articleInfo['category']; $returnArr['articles'][$key]['articleName'] = $articleInfo['articleName']; $returnArr['articles'][$key]['articleBody'] = $parser->parse(mb_substr($articleInfo['content'], 0, 300, 'utf-8')); $returnArr['articles'][$key]['postDate'] = $articleInfo['postDate']; } $returnArr['category'] = $categoryList; return $returnArr; }