示例#1
0
 /**
  * Render all category products
  *
  * @param CategoryInterface $category Category
  *
  * @return Response Response
  *
  * @Route(
  *      path = "category/{slug}/{id}",
  *      name = "store_category_products_list",
  *      requirements = {
  *          "slug" = "[a-zA-Z0-9-]+",
  *          "id" = "\d+"
  *      },
  *      methods = {"GET"}
  * )
  *
  * @AnnotationEntity(
  *      class = "elcodi.entity.category.class",
  *      name = "category",
  *      mapping = {
  *          "id" = "~id~",
  *          "enabled" = true,
  *      }
  * )
  */
 public function viewAction(CategoryInterface $category, $slug)
 {
     /**
      * We must check that the product slug is right. Otherwise we must
      * return a Redirection 301 to the right url
      */
     if ($slug !== $category->getSlug()) {
         return $this->redirectToRoute('store_category_products_list', ['id' => $category->getId(), 'slug' => $category->getSlug()], 301);
     }
     /**
      * @var CategoryRepository Category Repository
      * @var ProductRepository Product Repository
      */
     $categoryRepository = $this->get('elcodi.repository.category');
     $productRepository = $this->get('elcodi.repository.product');
     $categories = array_merge([$category], $categoryRepository->getChildrenCategories($category)->toArray());
     $products = $productRepository->getAllFromCategories($categories);
     return $this->renderTemplate('Pages:category-view.html.twig', ['products' => $products, 'category' => $category]);
 }
 /**
  * Removes the parent category for a root category.
  *
  * @param CategoryInterface $category The category.
  */
 private function removeParentCategoryForRootCategory(CategoryInterface $category)
 {
     if ($category->isRoot()) {
         $category->setParent(null);
     }
 }