PostController.php 697 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Repos\PostRepo;
  4. use Illuminate\Support\Facades\Redis;
  5. class PostController extends Controller
  6. {
  7. protected PostRepo $postRepo;
  8. public function __construct(PostRepo $postRepo)
  9. {
  10. $this->postRepo = $postRepo;
  11. }
  12. // 浏览文章
  13. public function show($id)
  14. {
  15. $post = $this->postRepo->getById($id);
  16. $views = $this->postRepo->addViews($post);
  17. return "Show Post #{$post->id}, Views: {$views}";
  18. }
  19. // 获取热门文章排行榜
  20. public function popular()
  21. {
  22. $posts = $this->postRepo->trending(10);
  23. if ($posts) {
  24. dump($posts->toArray());
  25. }
  26. }
  27. }