Reputation: 333
In ZF2 it is possible to combine ( extend ) navigation item using both array and database ?
I have root navigation ( depth level 1 and 2 ) which config reside as an php file navigation.config.php. Now there is one page that base on product category that will create a navigation for each category with a child page.
module
\-submodule
\-category 1
\-add
\-edit
\-form
\-list
\- ...
\-category n
It's tiresome to write everything as an array everytime I'm adding a category. Instead it will be good If I load it using code by reading the category from db than create a recursive mvc page for navigation.
All category actualy call same controller, but for UI sake I have to make it like above.
I test by creating a page using controller
public function listAction() {
$this->addPages(
array(
array(
'label' => 'R12313',
'route' => 'home',
'pages' => array(
array(
'label' => 'R12313cccc',
'route' => 'home',
)
)
),
array(
'label' => 'R123132',
'route' => 'home',
)
)
);
}
public function addPages($pages = array(), $min = 1, $max = 2) {
$navigation = $this->services->get('viewhelpermanager')->get('navigation');
$navigation = $navigation('navigation');
$active = $navigation->findActive($navigation->getContainer(), $min, $max);
if (isset($active['page'])) {
$active = $active['page'];
$pages = array('route' => 'home', 'pages' => $pages);
$newpage = \Zend\Navigation\Page\AbstractPage::factory($pages);
$newpage->setDefaultRouter($this->services->get('router'));
$active->addPages($newpage);
}
}
It comes out nicely but then I unable to set the menu to active on click for itself and the parent. Maybe doing it in Module bootstrap is better.
So any idea how ?
Upvotes: 1
Views: 207
Reputation: 333
Ok, here is my workaround to add page from controller. Maybe there is a better way
public function addPages($id, $pages = array()) {
$services = $this->getServiceLocator();
$navigation = $services->get('viewhelpermanager')->get('navigation');
$navigation = $navigation('navigation');
$active = $navigation->findById($id);
if ($active instanceof \Zend\Navigation\Page\AbstractPage) {
$navigationFactory = new \Zend\Navigation\Service\ConstructedNavigationFactory($pages);
$pages = $navigationFactory->getPages($services);
$active->addPages($pages);
}
}
You have to specify page id in the navigation.config.php though if you want to add it this way.
Upvotes: 1