CollectionController.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\LiquidRenderer;
  4. use Illuminate\Http\Request;
  5. use App\Models\SitePage;
  6. use App\Models\SitePageTag;
  7. class CollectionController extends Controller
  8. {
  9. protected $liquidRenderer;
  10. public function __construct(LiquidRenderer $liquidRenderer)
  11. {
  12. $this->liquidRenderer = $liquidRenderer;
  13. }
  14. public function index()
  15. {
  16. //模板支持多级目录,需要目录符号
  17. $output = LiquidRenderer::render('collection_list.liquid', [
  18. ]);
  19. return response($output);
  20. }
  21. public function detail($slug)
  22. {
  23. // Check if the $slug is numeric (ID), if so, fetch by ID
  24. if (is_numeric($slug)) {
  25. $tag = SitePageTag::find($slug); // Find by ID
  26. } else {
  27. // Otherwise, find by slug
  28. $tag = SitePageTag::where('slug', $slug)->first();
  29. }
  30. if (!$tag) {
  31. return response()->json(['message' => '标签未找到'], 404);
  32. }
  33. // 获取关联的 pages
  34. $pages = $tag->pages()->get();
  35. //模板支持多级目录,需要目录符号
  36. $output = LiquidRenderer::render('collection_list.liquid', [
  37. 'tag' => $tag,
  38. 'pages' => $pages,
  39. ]);
  40. return response($output);
  41. }
  42. }