ZF2动态菜单,带Zend\Navigation\Navigation [英] ZF2 Dynamic Menu with Zend\Navigation\Navigation
问题描述
我需要用Zend\Navigation\Navigation创建动态菜单?
在ZF1中,我做了这样:
$ container = new Zend_Navigation();
$ pages = array(
array(
'label'=>'Save',
'action'=>'save',
),
数组(
'label'=>'删除',
'action'=>'delete',
),
);
//添加两个页面
$ container-> addPages($ pages);
然后查看:
$这 - >导航() - >菜单();
但是在ZF2页面正在从配置中获取。
现在我创建\config\autoload\\\
av.global.php,这里创建页面数组。但是我需要在方法中执行页面数组并将其发送到导航帮助器,但是不知道如何((
我在我的控制器中这样做:
使用Zend\Navigation\Navigation;
$ pages = array(
//所有与导航相关的配置被收集在'导航'键
'navigation'=>数组(
//我们在(1)中配置的DefaultNavigationFactory使用'default'作为站点地图键
'default'= >数组(
//最后,这里是我们定义我们的页面层次结构
'account'=>数组(
'label'=>'faq',
'route'=>'faq',
'pages'=> array(
'news'=> array(
'label'=>'news',
'route'=>'news',
),
'manual'=>数组(
'label'=>'manual',
'route'=>'manual',
),
),
),
),
),
);
$ Menu = new Navigation($ pages);
然后看看:
$这 - >菜单() - >菜单();
但我有很多错误...
$ b $我认为你明白我的问题。
请帮忙。
对我的英文很抱歉
按照这些简单的步骤创建多级菜单动态步骤
1:创建一个部分menu.phtml文件并将其保存在布局文件夹或
模块中,例如application / view / layout / menu.phtml
< ul id =menu>
<?php foreach($ this-> container as $ page):?>
< li<?= $ page-> isActive()? 'class =active':'class =drop'?>>
<?php echo $ this-> navigation() - > menu() - > htmlify($ page)。 PHP_EOL?>
< div class =dropdown_2columns>
<?php foreach($ page as $ catpage):?>
< div class =col_1>
< h3<?= $ page-> isActive()? 'class =active':'class =drop'?> >
<?= $ this-> navigation() - > menu() - > htmlify($ catpage)。 PHP_EOL?>
< / h3>
< ul>
<?php foreach($ catpage as $ subpage):?>
< li<?= $ subpage-> isActive()? 'class =active':'class =drop'?>>
<?= $ this-> navigation() - > menu() - > htmlify($ subpage)。 PHP_EOL?>
<?php endforeach; ?>
< / ul>
< / div>
<?php endforeach; ?>
< / div><! - 结束下拉容器 - >
< / li>
<?php endforeach; ?>
< / ul>
您的module.php中的步骤2 $ / $ b
public function getServiceConfig()
{
return array(
'initializers'= > array(
function($ instance,$ sm){
if($ instance instanceof \Zend\Db\Adapter\AdapterAwareInterface){
$ instance-> setDbAdapter ($ sm-> get('Zend\Db\Adapter\Adapter'));
}
}
),
'invokables'=>数组(
'menu'=>'Application\Model\MenuTable',
),
'工厂'=>数组(
'Navigation'=>'Application\Navigation\YourNavigationFactory'
)
);
}
在您的模块中的src / navigation文件夹中创建YourNavigation.php和
YourNavigationFactory.php
命名空间Application\Navigation;
使用Zend\ServiceManager\ServiceLocatorInterface;
使用Zend\Navigation\Service\DefaultNavigationFactory;
使用Admin\Model\Entity\Tablepages;
class YourNavigation extends DefaultNavigationFactory
{
protected function getPages(ServiceLocatorInterface $ serviceLocator)
{
if(null === $ this-> pages){
$ fetchMenu = $ serviceLocator-> get('menu') - > fetchAll();
$ configuration ['navigation'] [$ this-> getName()] = array();
foreach($ fetchMenu as $ key => $ row)
{
$ subMenu = $ serviceLocator->获得( '菜单') - > fetchAllSubMenus($行[ '身份证']);
if($ subMenu){
$ pages = array();
foreach($ subMenu as $ k => $ v)
{
foreach($ v as $ field => $ value){
$ page ['label' ] = $ value ['heading'];
$ page ['route'] ='签证'
if($ value ['path'] == $ row ['path']){
$ page ['params'] = array('action'=>'index',
'category'=> $ this-> $ row ['path'],
);
}
$ subCatMenu = $ serviceLocator-> get('menu') - > fetchAllSubCatMenus($ value ['id']);
$ subcatpages = array();
$ subcatgroup = array();
$ group = array();
if($ subCatMenu> 0){
foreach($ subCatMenu as $ k => $ v)
{
foreach($ v as $ field = > $ value1){
$ subpage ['label'] = $ value1 ['heading'];
$ subpage ['route'] ='visas';
if($ value ['path'] == $ row ['path']){
$ subpage ['params'] = array('action'=>'index ',
'category'=> $ row ['path'],
'sub_category'=> $ value1 ['path']);
} elseif($ row ['id'] == 76){
$ subpage ['params'] = array('action'=>'index' ,
'category'=> $ value ['path'],
'sub_category'=> $ value1 ['path']);
} else {
$ subpage ['params'] = array('action'=>'index',
'category' = $ row ['path'],
'sub_category'=> $ value ['path'],
'id'=> $ value1 ['path']);
}
}
$ group [] = $ subpage;
}
$ page ['pages'] = $ group;
$ pages [] = $ page;
}
}
}
}
$ configuration ['navigation'] [$ this-> getName()] [$ row ['name']] = array(
'label'=> $行['name'],
'route'=>'签证',
'params'=>数组(
'action'=>'index' ,
'category'=> $ row ['path'],
),
'pages'=> $ pages,
);
}
如果(!isset($ configuration ['navigation'])){
throw new Exception\InvalidArgumentException('找不到导航配置密钥');
}
if(!isset($ configuration ['navigation'] [$ this-> getName()])){
throw new Exception\InvalidArgumentException(sprintf (
'无法通过名称%s找到导航容器,
$ this-> getName()
));
}
$ 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;
}
}
YourNavigationFactory.php
命名空间Application\Navigation;
使用Zend\ServiceManager\FactoryInterface;
使用Zend\ServiceManager\ServiceLocatorInterface;
class YourNavigationFactory实现FactoryInterface
{
public function createService(ServiceLocatorInterface $ serviceLocator)
{
$ navigation = new MyNavigation();
return $ navigation-> createService($ serviceLocator);
}
}
在您的布局。 phtml
<?php echo $ this-> navigation('navigation' >菜单() - > setPartial( '菜单') - >渲染(); ?>
从导航创建动态站点地图
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ > setUseXmlDeclaration(false)
- > setServerUrl('http://www.yourdomain.com')
- > setFormatOutput(true);?>
echo $ this-> navigation() - > menu() - > setMinDepth(null) - > setMaxDepth(null) - > setOnlyActiveBranch(false) - > setRenderInvisible );
创建面包屑
echo $ this-> navigation()
- > breadcrumbs()
- > setLinkLast(true )
- > setMaxDepth(1)
- > setSeparator('▶'。PHP_EOL);
我希望它有助于您节省时间
I want ask you about this problem. What do i need to create Dynamic Menu with Zend\Navigation\Navigation?
In ZF1 i made like this:
$container = new Zend_Navigation(); $pages = array( array( 'label' => 'Save', 'action' => 'save', ), array( 'label' => 'Delete', 'action' => 'delete', ), ); // add two pages $container->addPages($pages);
and then in view:
$this->navigation()->menu();
But in ZF2 pages are taking from config. Now i create \config\autoload\nav.global.php and here create page array. But i need to do page array in method and send it into navigation helper, but ii dont know how ((
i tried to do this in my controller:
use Zend\Navigation\Navigation; $pages =array( // All navigation-related configuration is collected in the 'navigation' key 'navigation' => array( // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key 'default' => array( // And finally, here is where we define our page hierarchy 'account' => array( 'label' => 'faq', 'route' => 'faq', 'pages' => array( 'news' => array( 'label' => 'news', 'route' => 'news', ), 'manual' => array( 'label' => 'manual', 'route' => 'manual', ), ), ), ), ), ); $Menu = new Navigation($pages);
and then this in view:
$this->Menu()->menu();
but i have a lot of mistakes...
i think you understand my problem. please help. sorry for my english.
解决方案Follow these easy steps to create the multilevel menu dynamically Step 1:create a partial menu.phtml file and save it in layout folder or the module,eg application/view/layout/menu.phtml
<ul id="menu" > <?php foreach ($this->container as $page): ?> <li <?= $page->isActive()? 'class="active"' : 'class="drop"' ?>> <?php echo $this->navigation()->menu()->htmlify($page). PHP_EOL ?> <div class="dropdown_2columns" > <?php foreach ($page as $catpage) :?> <div class="col_1" > <h3 <?= $page->isActive()? 'class="active"' : 'class="drop"' ?> > <?= $this->navigation()->menu()->htmlify($catpage). PHP_EOL ?> </h3> <ul > <?php foreach ($catpage as $subpage) :?> <li <?= $subpage->isActive()? 'class="active"' : 'class="drop"' ?>> <?= $this->navigation()->menu()->htmlify($subpage). PHP_EOL ?> <?php endforeach; ?> </ul> </div> <?php endforeach; ?> </div><!-- End dropdown container --> </li> <?php endforeach; ?> </ul>
Step 2 in your module.php
public function getServiceConfig() { return array( 'initializers' => array( function ($instance, $sm) { if ($instance instanceof \Zend\Db\Adapter\AdapterAwareInterface) { $instance->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter')); } } ), 'invokables' => array( 'menu' => 'Application\Model\MenuTable', ), 'factories' => array( 'Navigation' => 'Application\Navigation\YourNavigationFactory' ) ); }
in src/navigation folder in your module create YourNavigation.php and YourNavigationFactory.php
namespace Application\Navigation; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\Navigation\Service\DefaultNavigationFactory; use Admin\Model\Entity\Tablepages; class YourNavigation extends DefaultNavigationFactory { protected function getPages(ServiceLocatorInterface $serviceLocator) { if (null === $this->pages) { $fetchMenu = $serviceLocator->get('menu')->fetchAll(); $configuration['navigation'][$this->getName()] = array(); foreach($fetchMenu as $key=>$row) { $subMenu = $serviceLocator->get('menu')->fetchAllSubMenus($row['id']); if($subMenu){ $pages = array(); foreach($subMenu as $k=>$v) { foreach($v as $field=>$value){ $page['label'] =$value['heading']; $page['route'] = 'visas'; if ($value['path'] == $row['path']){ $page['params'] = array('action'=>'index', 'category'=> $this->$row['path'], ); } $subCatMenu = $serviceLocator->get('menu')->fetchAllSubCatMenus($value['id']); $subcatpages = array(); $subcatgroup = array(); $group = array(); if($subCatMenu>0){ foreach($subCatMenu as $k=>$v) { foreach($v as $field=>$value1){ $subpage['label'] =$value1['heading']; $subpage['route'] = 'visas'; if ($value['path'] ==$row['path']){ $subpage['params'] = array('action'=>'index', 'category'=> $row['path'], 'sub_category'=> $value1['path']); }elseif($row['id'] ==76){ $subpage['params'] = array('action'=>'index', 'category'=>$value['path'], 'sub_category'=>$value1['path']); }else{ $subpage['params'] = array('action'=>'index', 'category'=> $row['path'], 'sub_category'=> $value['path'], 'id'=> $value1['path']); } } $group[] =$subpage; } $page['pages'] =$group; $pages[] =$page; } } } } $configuration['navigation'][$this->getName()][$row['name']] = array( 'label' => $row['name'], 'route' => 'visas', 'params' => array( 'action' => 'index', 'category' => $row['path'], ), 'pages' => $pages, ); } if (!isset($configuration['navigation'])) { throw new Exception\InvalidArgumentException('Could not find navigation configuration key'); } if (!isset($configuration['navigation'][$this->getName()])) { throw new Exception\InvalidArgumentException(sprintf( 'Failed to find a navigation container by the name "%s"', $this->getName() )); } $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; } }
YourNavigationFactory.php
namespace Application\Navigation; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class YourNavigationFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { $navigation = new MyNavigation(); return $navigation->createService($serviceLocator); } }
in your layout.phtml
<?php echo $this->navigation('navigation')->menu()->setPartial('menu')->render(); ?>
to create dynamic sitemap from navigation
$this->navigation('navigation') ->sitemap() ->setUseXmlDeclaration(false) ->setServerUrl('http://www.yourdomain.com') ->setFormatOutput(true);?> echo $this->navigation()->menu()->setMinDepth(null)->setMaxDepth(null)->setOnlyActiveBranch(false)->setRenderInvisible(true);
to create breadcrumbs
echo $this->navigation() ->breadcrumbs() ->setLinkLast(true) ->setMaxDepth(1) ->setSeparator(' ▶' . PHP_EOL);
I Hope it helps you to save your time
这篇关于ZF2动态菜单,带Zend\Navigation\Navigation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!