Symfony2 SonataBundle显示类别

时间:2015-04-16 13:22:01

标签: php symfony

我正在尝试在前端侧栏上显示Admin中创建的类别。我没有找到任何关于此的信息,所以我试着即兴发挥。我试图从sonatas电子商务包中的CatalogController复制代码并修改它。我没有收到任何错误,页面加载;但是,类别仍未显示。为什么呢?

这是控制器:

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,
        ));
    }

我尝试显示如下类别:

{% 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 %}

1 个答案:

答案 0 :(得分:0)

您正在检索单个类别而不是所有类别。在您的控制器中,您应该返回:

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