BlogController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // app/Http/Controllers/BlogController.php
  3. namespace App\Http\Controllers;
  4. use App\Models\Post;
  5. use App\Models\Category;
  6. use App\Models\Tag;
  7. use App\Models\ProgressChapter;
  8. use Illuminate\Http\Request;
  9. use Carbon\Carbon;
  10. use App\Http\Api\UserApi;
  11. use Illuminate\Support\Facades\Log;
  12. class BlogController extends Controller
  13. {
  14. protected $categories = [
  15. ['id' => 'sutta', 'label' => 'sutta'],
  16. ['id' => 'vinaya', 'label' => 'vinaya'],
  17. ['id' => 'abhidhamma', 'label' => 'abhidhamma'],
  18. ];
  19. // 首页 - 最新博文列表
  20. public function index($user)
  21. {
  22. $user = UserApi::getByName($user);
  23. $posts = ProgressChapter::with('channel')
  24. ->where('progress', '>', 0.9)
  25. ->whereHas('channel', function ($query) use ($user) {
  26. $query->where('status', 30)->where('owner_uid', $user['id']);
  27. })
  28. ->latest()
  29. ->paginate(10);
  30. $categories = $this->categories;
  31. /*
  32. $posts = Post::published()
  33. ->with(['category', 'tags'])
  34. ->latest()
  35. ->paginate(10);
  36. $categories = Category::withCount('posts')->get();
  37. $popularPosts = Post::published()
  38. ->orderBy('views_count', 'desc')
  39. ->take(5)
  40. ->get();
  41. */
  42. //return view('blog.index', compact('posts', 'categories', 'popularPosts'));
  43. return view('blog.index', compact('user', 'posts', 'categories'));
  44. }
  45. /*
  46. // 博文详情页
  47. public function show(Post $post)
  48. {
  49. if (!$post->is_published) {
  50. abort(404);
  51. }
  52. $post->incrementViews();
  53. $post->load(['category', 'tags']);
  54. // 相关文章
  55. $relatedPosts = Post::published()
  56. ->where('category_id', $post->category_id)
  57. ->where('id', '!=', $post->id)
  58. ->take(3)
  59. ->get();
  60. // 上一篇和下一篇
  61. $prevPost = Post::published()
  62. ->where('published_at', '<', $post->published_at)
  63. ->latest()
  64. ->first();
  65. $nextPost = Post::published()
  66. ->where('published_at', '>', $post->published_at)
  67. ->oldest()
  68. ->first();
  69. return view('blog.show', compact('post', 'relatedPosts', 'prevPost', 'nextPost'));
  70. }
  71. // 分类列表
  72. public function categories()
  73. {
  74. $categories = Category::withCount('posts')
  75. ->having('posts_count', '>', 0)
  76. ->orderBy('posts_count', 'desc')
  77. ->get();
  78. return view('blog.categories', compact('categories'));
  79. }
  80. */
  81. // 分类下的文章
  82. public function category(
  83. $user,
  84. $category1,
  85. $category2 = null,
  86. $category3 = null,
  87. $category4 = null,
  88. $category5 = null
  89. ) {
  90. $user = UserApi::getByName($user);
  91. $posts = [];
  92. $categories = $this->categories;
  93. return view('blog.category', compact('user', 'categories', 'posts'));
  94. }
  95. /*
  96. // 年度归档
  97. public function archives()
  98. {
  99. $archives = Post::published()
  100. ->selectRaw('YEAR(published_at) as year, COUNT(*) as count')
  101. ->groupBy('year')
  102. ->orderBy('year', 'desc')
  103. ->get();
  104. return view('blog.archives', compact('archives'));
  105. }
  106. // 指定年份的文章
  107. public function archivesByYear($year)
  108. {
  109. $posts = Post::published()
  110. ->whereYear('published_at', $year)
  111. ->with(['category', 'tags'])
  112. ->latest()
  113. ->paginate(15);
  114. // 按月分组
  115. $postsByMonth = $posts->getCollection()->groupBy(function ($post) {
  116. return $post->published_at->format('Y-m');
  117. });
  118. return view('blog.archives-year', compact('posts', 'postsByMonth', 'year'));
  119. }
  120. // 标签页面
  121. public function tag(Tag $tag)
  122. {
  123. $posts = $tag->posts()
  124. ->published()
  125. ->with(['category', 'tags'])
  126. ->latest()
  127. ->paginate(10);
  128. return view('blog.tag', compact('tag', 'posts'));
  129. }
  130. // 搜索
  131. public function search(Request $request)
  132. {
  133. $query = $request->get('q');
  134. if (empty($query)) {
  135. return redirect()->route('blog.index');
  136. }
  137. $posts = Post::published()
  138. ->where(function ($q) use ($query) {
  139. $q->where('title', 'LIKE', "%{$query}%")
  140. ->orWhere('content', 'LIKE', "%{$query}%")
  141. ->orWhere('excerpt', 'LIKE', "%{$query}%");
  142. })
  143. ->with(['category', 'tags'])
  144. ->latest()
  145. ->paginate(10);
  146. return view('blog.search', compact('posts', 'query'));
  147. }
  148. */
  149. }