dongmao3131 2014-07-22 11:26
浏览 19

将参数传递给Navigation Factory

I want to create Categories menu from database records, but limit the menu items with current category and it's subcategories (like on Walmart.com). The catch is how to access current category in Navigation Factory where I create the pages for the menu?

Navigation factory:

use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Navigation\Service\DefaultNavigationFactory;

class CategoriesMenu extends DefaultNavigationFactory
{
    protected function getPages(ServiceLocatorInterface $serviceLocator)
    {
    if (null === $this->pages) {
        $em = $serviceLocator->get('Doctrine\ORM\EntityManager');

        // here I need to know the current category

        $categories = $em->getRepository('Application\Entity\Category')->getRootCategories();

        $nav = array();
        foreach ($categories as $category) {
        $page = array(
           'label' => $category->getName(),
           'route' => 'category',
           'params' => array(
               'category_path' => $category->getUrlName(),
            ),
           'pages' => array(),
           );
           $nav[] = $page;
        }
        $configuration['navigation'][$this->getName()] = $nav;

        $application = $serviceLocator->get('Application');
        $routeMatch  = $application->getMvcEvent()->getRouteMatch();
        $router      = $application->getMvcEvent()->getRouter();
        $pages       = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]);

        $this->pages = $this->injectComponents($pages, $routeMatch, $router);
    }
    return $this->pages;
    }
}

Categories menu factory:

use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Navigation\Service\DefaultNavigationFactory;

class CategoriesMenuFactory extends DefaultNavigationFactory
{
    protected function getName()
    {
    return 'categoriesmenu';
    }

    public function createService(ServiceLocatorInterface $serviceLocator)
    {
    $navigation = new CategoriesMenu();
    return $navigation->createService($serviceLocator);
    }
}

module.config.php

'service_manager' => array(
    'factories' => array(
        'categoriesmenu' => 'Application\Navigation\Service\CategoriesMenuFactory',
    ),
),
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python变量和列表之间的相互影响
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)