// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\blog\controller; use ywxapp\controller\FrontendBase; use addon\blog\model\BlogArticle as ArticleModel; use think\facade\Config; use think\facade\Response; use think\facade\View; /** * Rss 类 * * @author ywxapp */ class Rss extends FrontendBase { /** * Summary of needLogin * @var array */ protected $noNeedLogin = ['*']; /** * Summary of needRight * @var array */ protected $noNeedVerify = ['*']; /** * 控制器初始化 _initialize * @return void */ protected function initialize() {} /** * RSS订阅页面 */ public function index() { $page = Request::param('page', 1); $pageSize = 20; $articles = ArticleModel::with(['user', 'category']) ->where('status', 1) ->order('create_at', 'desc') ->page($page, $pageSize) ->select(); $siteName = Config::get('site.name', 'TP-Blog'); $siteUrl = Config::get('site.url', 'http://localhost'); $description = Config::get('site.description', 'TP-Blog是一个基于ThinkPHP的博客系统'); $xml = $this->generateRssXml($articles, $siteName, $siteUrl, $description); return Response::create($xml, 'xml'); } /** * 生成RSS XML */ private function generateRssXml($articles, $siteName, $siteUrl, $description) { $xml = ''; $xml .= ''; $xml .= ''; $xml .= '' . htmlspecialchars($siteName) . ''; $xml .= '' . htmlspecialchars($siteUrl) . ''; $xml .= '' . htmlspecialchars($description) . ''; $xml .= 'zh-cn'; $xml .= '' . date('r') . ''; $xml .= ''; foreach ($articles as $article) { $xml .= ''; $xml .= '' . htmlspecialchars($article->title) . ''; $xml .= '' . htmlspecialchars($siteUrl . '/article/' . $article->id) . ''; $xml .= '' . htmlspecialchars($article->summary ?: strip_tags(mb_substr($article->content, 0, 200))) . ''; $xml .= '' . date('r', $article->create_at) . ''; $xml .= '' . htmlspecialchars($article->user->nickname) . ''; $xml .= '' . htmlspecialchars($article->category->name) . ''; $xml .= '' . htmlspecialchars($siteUrl . '/article/' . $article->id) . ''; $xml .= ''; } $xml .= ''; $xml .= ''; return $xml; } /** * RSS阅读器页面 */ public function reader() { $siteName = Config::get('site.name', 'TP-Blog'); $rssUrl = Config::get('site.url', 'http://localhost') . '/rss'; return View::fetch('index/rss/reader', [ 'siteName' => $siteName, 'rssUrl' => $rssUrl ]); } }