BlogController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. Log::info($posts[0]->formatted_created_at);
  31. $categories = $this->categories;
  32. /*
  33. $posts = Post::published()
  34. ->with(['category', 'tags'])
  35. ->latest()
  36. ->paginate(10);
  37. $categories = Category::withCount('posts')->get();
  38. $popularPosts = Post::published()
  39. ->orderBy('views_count', 'desc')
  40. ->take(5)
  41. ->get();
  42. */
  43. //return view('blog.index', compact('posts', 'categories', 'popularPosts'));
  44. return view('blog.index', compact('user', 'posts', 'categories'));
  45. }
  46. /*
  47. // 博文详情页
  48. public function show(Post $post)
  49. {
  50. if (!$post->is_published) {
  51. abort(404);
  52. }
  53. $post->incrementViews();
  54. $post->load(['category', 'tags']);
  55. // 相关文章
  56. $relatedPosts = Post::published()
  57. ->where('category_id', $post->category_id)
  58. ->where('id', '!=', $post->id)
  59. ->take(3)
  60. ->get();
  61. // 上一篇和下一篇
  62. $prevPost = Post::published()
  63. ->where('published_at', '<', $post->published_at)
  64. ->latest()
  65. ->first();
  66. $nextPost = Post::published()
  67. ->where('published_at', '>', $post->published_at)
  68. ->oldest()
  69. ->first();
  70. return view('blog.show', compact('post', 'relatedPosts', 'prevPost', 'nextPost'));
  71. }
  72. // 分类列表
  73. public function categories()
  74. {
  75. $categories = Category::withCount('posts')
  76. ->having('posts_count', '>', 0)
  77. ->orderBy('posts_count', 'desc')
  78. ->get();
  79. return view('blog.categories', compact('categories'));
  80. }
  81. */
  82. // 分类下的文章
  83. public function category(
  84. $user,
  85. $category1,
  86. $category2 = null,
  87. $category3 = null,
  88. $category4 = null,
  89. $category5 = null
  90. ) {
  91. $user = UserApi::getByName($user);
  92. $posts = [];
  93. $categories = $this->categories;
  94. return view('blog.category', compact('user', 'categories', 'posts'));
  95. }
  96. /*
  97. // 年度归档
  98. public function archives()
  99. {
  100. $archives = Post::published()
  101. ->selectRaw('YEAR(published_at) as year, COUNT(*) as count')
  102. ->groupBy('year')
  103. ->orderBy('year', 'desc')
  104. ->get();
  105. return view('blog.archives', compact('archives'));
  106. }
  107. // 指定年份的文章
  108. public function archivesByYear($year)
  109. {
  110. $posts = Post::published()
  111. ->whereYear('published_at', $year)
  112. ->with(['category', 'tags'])
  113. ->latest()
  114. ->paginate(15);
  115. // 按月分组
  116. $postsByMonth = $posts->getCollection()->groupBy(function ($post) {
  117. return $post->published_at->format('Y-m');
  118. });
  119. return view('blog.archives-year', compact('posts', 'postsByMonth', 'year'));
  120. }
  121. // 标签页面
  122. public function tag(Tag $tag)
  123. {
  124. $posts = $tag->posts()
  125. ->published()
  126. ->with(['category', 'tags'])
  127. ->latest()
  128. ->paginate(10);
  129. return view('blog.tag', compact('tag', 'posts'));
  130. }
  131. // 搜索
  132. public function search(Request $request)
  133. {
  134. $query = $request->get('q');
  135. if (empty($query)) {
  136. return redirect()->route('blog.index');
  137. }
  138. $posts = Post::published()
  139. ->where(function ($q) use ($query) {
  140. $q->where('title', 'LIKE', "%{$query}%")
  141. ->orWhere('content', 'LIKE', "%{$query}%")
  142. ->orWhere('excerpt', 'LIKE', "%{$query}%");
  143. })
  144. ->with(['category', 'tags'])
  145. ->latest()
  146. ->paginate(10);
  147. return view('blog.search', compact('posts', 'query'));
  148. }
  149. */
  150. }