ProductController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Services\LiquidRenderer;
  5. use App\Models\DistProduct;
  6. use App\Models\DistProductCategory; // 引入分类模型
  7. class ProductController extends Controller
  8. {
  9. protected $liquidRenderer;
  10. public function __construct(LiquidRenderer $liquidRenderer)
  11. {
  12. $this->liquidRenderer = $liquidRenderer;
  13. }
  14. /**
  15. * Display a listing of the products.
  16. *
  17. * @return \Illuminate\Http\Response
  18. */
  19. // public function index()
  20. // {
  21. // $products = Product::paginate(10); // 每页显示10个产品
  22. // return $this->liquidRenderer->render('products.index', ['products' => $products]);
  23. // }
  24. public function category($slug)
  25. {
  26. // $products = DistProduct::paginate(10); // 每页显示10个产品
  27. // return $this->liquidRenderer->render('products_categories.liquid', ['products' => $products]);
  28. // 获取分类信息
  29. $category = DistProductCategory::findOrFail($slug);
  30. // 获取分类下的所有产品,并进行分页
  31. $products = DistProduct::where('category_id', $category->id)->paginate(10);
  32. // 渲染模板并传递数据
  33. return $this->liquidRenderer->render('products_categories.liquid', [
  34. 'category' => $category, // 分类名称
  35. 'products' => $products, // 分类下的产品
  36. ]);
  37. }
  38. /**
  39. * Display the specified product.
  40. *
  41. * @param int $id
  42. * @return \Illuminate\Http\Response
  43. */
  44. public function detail($id)
  45. {
  46. $product = DistProduct::getProduct($id);
  47. return $this->liquidRenderer->render('products_detail.liquid',
  48. [
  49. 'product' => $product,
  50. 'csrf_token' => csrf_token()
  51. ]);
  52. }
  53. }