ZF2:导航中动态创建的子页面不会变为活动状态(isActive()== false)
ZF2: Dynamically created sub pages in navigation doesn't become active (isActive() == false)
我有以下问题...
这是我的导航:
return array(
'navigation' => array(
'site' => array(
array(
'label' => 'Home',
'route' => 'site',
),
'categories' => array(
'label' => 'Categories',
'class' => 'categories',
'uri' => '#',
),
'contact-us' => array(
'label' => 'Contact Us',
'route' => 'site/contact-us',
),
),
),
);
在我的 Module.php 中我有函数:
public function attachSubMenu($serviceManager) {
$siteNavigation = $serviceManager->get('siteNavigation');
$router = $serviceManager->get('router');
$categoriesTable = $serviceManager->get('Category/Model/Table/CategoriesTable');
$categories = $categoriesTable->fetchAll();
$categoriesRoute = $siteNavigation->findByClass('categories');
if (!is_null($categoriesRoute)) {
$pages = array();
foreach ($categories as $category) {
$newPage = new Mvc(array(
'label' => $category->name,
'route' => 'site/categories',
'params' => array(
'category' => $category->route
),
));
$newPage->setRouter($router);
array_push($pages, $newPage);
}
$categoriesRoute->addPages($pages);
}
}
到目前为止一切顺利。现在我有带有类别项的菜单和带有类别的列表作为类别的子菜单。但是当我单击其中一个列出的类别时,子菜单和父元素都没有 class "active"。当我点击主页时,我有 class "active",所以我认为问题出在类别列表中。
这是我在布局中显示导航的方式:
<?= $this->navigation('siteNavigation')->menu()->setMaxDepth(1); ?>
我调试后发现,当我在 Zend\Navigation\Page\Mvc -> isActive 函数 $this->getRoute() 中动态生成这些类别时,不会 return 类别路由...如果我添加类别像那样:
return array(
'navigation' => array(
'site' => array(
array(
'label' => 'Home',
'route' => 'site',
),
'categories' => array(
'label' => 'Categories',
'class' => 'categories',
'uri' => '#',
'pages' => array(
array(
'label' => 'Cat1',
'route' => 'site/categories',
'params' => array(
'category' => 'cat1'
),
//.....
),
),
),
'contact-us' => array(
'label' => 'Contact Us',
'route' => 'site/contact-us',
),
),
),
);
一切正常。子菜单和父菜单有 class "active".....
知道如何解决这个问题吗?
我想我找到了解决方案...
新页面没有设置RouteMatch,所以我在后面加了一行:
$newPage->setRouter($router);
$newPage->setRouteMatch($routeMatch);//The new line;
然后我的导航按预期工作。
我有以下问题...
这是我的导航:
return array(
'navigation' => array(
'site' => array(
array(
'label' => 'Home',
'route' => 'site',
),
'categories' => array(
'label' => 'Categories',
'class' => 'categories',
'uri' => '#',
),
'contact-us' => array(
'label' => 'Contact Us',
'route' => 'site/contact-us',
),
),
),
);
在我的 Module.php 中我有函数:
public function attachSubMenu($serviceManager) {
$siteNavigation = $serviceManager->get('siteNavigation');
$router = $serviceManager->get('router');
$categoriesTable = $serviceManager->get('Category/Model/Table/CategoriesTable');
$categories = $categoriesTable->fetchAll();
$categoriesRoute = $siteNavigation->findByClass('categories');
if (!is_null($categoriesRoute)) {
$pages = array();
foreach ($categories as $category) {
$newPage = new Mvc(array(
'label' => $category->name,
'route' => 'site/categories',
'params' => array(
'category' => $category->route
),
));
$newPage->setRouter($router);
array_push($pages, $newPage);
}
$categoriesRoute->addPages($pages);
}
}
到目前为止一切顺利。现在我有带有类别项的菜单和带有类别的列表作为类别的子菜单。但是当我单击其中一个列出的类别时,子菜单和父元素都没有 class "active"。当我点击主页时,我有 class "active",所以我认为问题出在类别列表中。
这是我在布局中显示导航的方式:
<?= $this->navigation('siteNavigation')->menu()->setMaxDepth(1); ?>
我调试后发现,当我在 Zend\Navigation\Page\Mvc -> isActive 函数 $this->getRoute() 中动态生成这些类别时,不会 return 类别路由...如果我添加类别像那样:
return array(
'navigation' => array(
'site' => array(
array(
'label' => 'Home',
'route' => 'site',
),
'categories' => array(
'label' => 'Categories',
'class' => 'categories',
'uri' => '#',
'pages' => array(
array(
'label' => 'Cat1',
'route' => 'site/categories',
'params' => array(
'category' => 'cat1'
),
//.....
),
),
),
'contact-us' => array(
'label' => 'Contact Us',
'route' => 'site/contact-us',
),
),
),
);
一切正常。子菜单和父菜单有 class "active".....
知道如何解决这个问题吗?
我想我找到了解决方案...
新页面没有设置RouteMatch,所以我在后面加了一行:
$newPage->setRouter($router);
$newPage->setRouteMatch($routeMatch);//The new line;
然后我的导航按预期工作。