将参数传递给Navigation Factory

时间:2014-07-22 11:26:14

标签: php zend-framework2 zend-navigation

我想从数据库记录创建“类别”菜单,但是限制具有当前类别及其子类别的菜单项(如在Walmart.com上)。问题是如何在导航工厂中访问当前类别,我在其中为菜单创建页面?

导航工厂:     

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

分类菜单工厂:     

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 个答案:

没有答案