Php 将参数传递到导航工厂
我想从数据库记录中创建Categories菜单,但用当前类别及其子类别限制菜单项(如在Walmart.com上)。问题是如何在导航工厂中访问当前类别,我在那里为菜单创建页面 导航工厂:Php 将参数传递到导航工厂,php,zend-framework2,zend-navigation,Php,Zend Framework2,Zend Navigation,我想从数据库记录中创建Categories菜单,但用当前类别及其子类别限制菜单项(如在Walmart.com上)。问题是如何在导航工厂中访问当前类别,我在那里为菜单创建页面 导航工厂: use Zend\ServiceManager\ServiceLocatorInterface; use Zend\Navigation\Service\DefaultNavigationFactory; class CategoriesMenu extends DefaultNavigationFactory
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);
}
}
类别菜单工厂:
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',
),
),
如果“当前类别”是管线参数,则它将位于管线匹配对象内。您应该能够通过
$routeMatch->getParam('category')
访问它。如果是GET或POST,您可以从请求实例获取它。谢谢,我找到了从route获取此参数的方法,但理想情况下,我希望将其从控制器传递到导航,因为现在我必须在控制器和导航工厂中加载两次category对象。