app/Customize/Controller/CategoryController.php line 33

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Customize\Repository\ProductRepository;
  4. use Eccube\Repository\CategoryRepository;
  5. use Eccube\Repository\NewsRepository;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. class CategoryController extends BaseController
  13. {
  14. protected $categoryRepository;
  15. protected $productRepository;
  16. public function __construct(
  17. CategoryRepository $categoryRepository,
  18. ProductRepository $productRepository
  19. ) {
  20. $this->categoryRepository = $categoryRepository;
  21. $this->productRepository = $productRepository;
  22. }
  23. /**
  24. * @Method("GET")
  25. * @Route("/category/{id}")
  26. * @Template("@user_data/category/index.twig")
  27. */
  28. public function category($id)
  29. {
  30. $category = $this->categoryRepository->find($id);
  31. if (!$category) {
  32. throw new NotFoundHttpException();
  33. }
  34. return ['category' => $category];
  35. }
  36. /**
  37. * @Route("/category-products/{categoryID}", methods={"GET"})
  38. * @return JsonResponse
  39. */
  40. public function getProductIncategory($categoryID)
  41. {
  42. $products = $this->productRepository->getProductIncategory($categoryID);
  43. $productArray = [];
  44. foreach ($products as $product) {
  45. $productArray[] = [
  46. 'id' => $product->getId(),
  47. 'name' => $product->getName(),
  48. 'image' => $product->getMainFileName()['file_name'] ?? null
  49. ];
  50. }
  51. return $this->json(['products' => $productArray]);
  52. }
  53. /**
  54. * @Route("/category-sample-products/{categoryID}", methods={"GET"})
  55. * @return JsonResponse
  56. */
  57. public function getSampleProductIncategory($categoryID)
  58. {
  59. $products = $this->productRepository->getSampleProductInCategory($categoryID);
  60. $productArray = [];
  61. foreach ($products as $product) {
  62. $productArray[] = [
  63. 'id' => $product->getId(),
  64. 'name' => $product->getName(),
  65. 'image' => $product->getMainFileName() ? $product->getMainFileName()['file_name'] : null,
  66. 'price' => $product->getPrice02Min(),
  67. 'note' => $product->getDescriptionDetail(),
  68. ];
  69. }
  70. return $this->json(['products' => $productArray]);
  71. }
  72. }