Php 显示类别的Symfony2 SonataBundle

Php 显示类别的Symfony2 SonataBundle,php,symfony,Php,Symfony,我试图在前端侧边栏的管理中显示创建的类别。我没有找到任何关于这方面的信息,所以我试着即兴创作。我试图从sonatas电子商务包中的CatalogController复制代码并对其进行修改。我没有得到任何错误,页面加载;但是,类别仍不显示。为什么? 这是控制器: public function sidebarAction() { $page = $this->getRequest()->get('page', 1); $displa

我试图在前端侧边栏的管理中显示创建的类别。我没有找到任何关于这方面的信息,所以我试着即兴创作。我试图从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 %}

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

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

输入错误:$categoryName=$this->getRequest->get'category_name';不是$categorySlug=$this->getRequest->get'category_name';你的代码成功了。是否可以只选择和显示类别,而不显示子类别?是的,您只需要在控制器中过滤它们,然后再返回它们。我不知道分类经理是怎么工作的不幸的是。。。