简体   繁体   中英

Symfony2 SonataBundle displaying the categories

I am trying to display created Categories in Admin on the frontends sidebar. I had not found any information regarding this, so I tried to improvise. I tried to copy the code from CatalogController in sonatas Ecommerce bundle and modify it. I am not getting any errors, and the page loads; however, the categories are still not displayed. Why?

This is the controller:

public function sidebarAction()
    {
        $page        = $this->getRequest()->get('page', 1);
        $displayMax  = $this->getRequest()->get('max', 9);
        $displayMode = $this->getRequest()->get('mode', 'grid');
        $filter      = $this->getRequest()->get('filter');
        $option      = $this->getRequest()->get('option');

        if (!in_array($displayMode, array('grid'))) { // "list" mode will be added later
            throw new NotFoundHttpException(sprintf('Given display_mode "%s" doesn\'t exist.', $displayMode));
        }

        $category = $this->retrieveCategoryFromQueryString();



        return $this->render('sidebar.html.twig', array(
            'display_mode' => $displayMode,         
            'category'     => $category,
            'provider'     => $this->getProviderFromCategory($category),
        ));
    }


             /*  $em = $this->getDoctrine()->getManager();
            $products = $em->getRepository('MpShopBundle:Product')->findAll();
               return $this->render('sidebar.html.twig',  array(
               'products'=>$products       
               )); */

    /**
     * Retrieve Category from its id and slug, if any.
     *
     * @return CategoryInterface|null
     */
    protected function retrieveCategoryFromQueryString()
    {
        $categoryId   = $this->getRequest()->get('category_id');
        $categorySlug = $this->getRequest()->get('category_slug');
        $categorySlug = $this->getRequest()->get('category_name');

        if (!$categoryId || !$categorySlug || $categoryName) {
            return null;
        }

        return $this->getCategoryManager()->findOneBy(array(
            'id'      => $categoryId,
            'name'  => $categoryName,
            'enabled' => true,
        ));
    }

I try to display the categories like this:

{% for categories in category %}

                <li class="subMenu"><a> {{ categories.name }} [{{ categories|length }}]</a>
                <ul>
                    <li><a href="{{ path('products') }}">{{ categories.name }} ({{ categories|length }})</a></li>

                </ul>
                </li>

            {% endfor %}

You're retrieving a single category and not all of them. In your controller you should return:

    return $this->render('sidebar.html.twig', array(
        'display_mode' => $displayMode,         
        'categories'   => $this->getCategoryManager()->findAll(),
        'provider'     => $this->getProviderFromCategory($category),
    ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM