ZF2 动态菜单与 ZendNavigationNavigation
我想问你这个问题.使用 ZendNavigationNavigation 创建动态菜单需要什么?
在 ZF1 中我是这样制作的:
$container = new Zend_Navigation();$页=数组(大批('标签' =>'保存','动作' =>'保存',),大批('标签' =>'删除','动作' =>'删除',),);//添加两个页面$container->addPages($pages);
然后在视野中:
$this->navigation()->menu();
但在 ZF2 中,页面取自配置.现在我创建 configautoload av.global.php 并在这里创建页面数组.但是我需要在方法中做页面数组并将其发送到导航助手,但我不知道如何((
我试图在我的控制器中这样做:
使用 ZendNavigationNavigation;$页=数组(//所有与导航相关的配置都收集在 'navigation' 键中'导航' =>大批(//我们在 (1) 中配置的 DefaultNavigationFactory 使用 'default' 作为站点地图键'默认' =>大批(//最后,这里是我们定义页面层次结构的地方'帐户' =>大批('标签' =>'常问问题','路线' =>'常问问题','页面' =>大批('新闻' =>大批('标签' =>'消息','路线' =>'消息',),'手动' =>大批('标签' =>'手动的','路线' =>'手动的',),),),),),);$Menu = new Navigation($pages);
然后在视图中:
$this->Menu()->menu();
但我有很多错误......
我想你理解我的问题.请帮忙.对不起我的英语.
解决方案按照这些简单的步骤动态创建多级菜单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 类="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 步
公共函数 getServiceConfig(){返回数组('初始化器' =>大批(函数 ($instance, $sm) {if ($instance instanceof endDbAdapterAdapterAwareInterface) {$instance->setDbAdapter($sm->get('ZendDbAdapterAdapter'));}}),'invokables' =>大批('菜单' =>'应用程序模型菜单表',),'工厂' =>大批('导航' =>'应用程序导航你的导航工厂'));}
<块引用>
在模块的 src/navigation 文件夹中创建 YourNavigation.php 和YourNavigationFactory.php
命名空间应用导航;使用 ZendServiceManagerServiceLocatorInterface;使用 ZendNavigationServiceDefaultNavigationFactory;使用 AdminModelEntityTablepages;类 YourNavigation 扩展 DefaultNavigationFactory{受保护的函数 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']);如果($subMenu){$pages = 数组();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','类别'=>$this->$row['path'],);}$subCatMenu = $serviceLocator->get('menu')->fetchAllSubCatMenus($value['id']);$subcatpages = 数组();$subcatgroup = 数组();$group = 数组();如果($subCatMenu>0){foreach($subCatMenu as $k=>$v){foreach($v as $field=>$value1){$subpage['label'] =$value1['heading'];$subpage['route'] = '签证';if ($value['path'] ==$row['path']){$subpage['params'] = array('action'=>'index','类别'=>$行['路径'],'sub_category'=>$value1['path']);}elseif($row['id'] ==76){$subpage['params'] = array('action'=>'index','类别'=>$值['路径'],'sub_category'=>$value1['path']);}别的{$subpage['params'] = array('action'=>'index','类别'=>$行['路径'],'sub_category'=>$值['路径'],'id'=>$value1['path']);}}$group[] =$subpage;}$page['pages'] =$group;$pages[] =$page;}}}}$configuration['navigation'][$this->getName()][$row['name']] = array('标签' =>$row['name'],'路线' =>签证",'参数' =>大批('动作' =>'指数','类别' =>$行['路径'],),'页面' =>$页,);}if (!isset($configuration['navigation'])) {throw new ExceptionInvalidArgumentException('找不到导航配置键');}if (!isset($configuration['navigation'][$this->getName()])) {抛出新异常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);}返回 $this->pages;}}
<块引用>
YourNavigationFactory.php
命名空间 ApplicationNavigation;使用 ZendServiceManagerFactoryInterface;使用 ZendServiceManagerServiceLocatorInterface;类 YourNavigationFactory 实现 FactoryInterface{公共函数 createService(ServiceLocatorInterface $serviceLocator){$navigation = new MyNavigation();return $navigation->createService($serviceLocator);}}
<块引用>
在你的 layout.phtml 中
navigation('navigation')->menu()->setPartial('menu')->render();?>
<块引用>
从导航创建动态站点地图
$this->navigation('navigation')->站点地图()->setUseXmlDeclaration(false)->setServerUrl('http://www.yourdomain.com')->setFormatOutput(true);?>echo $this->navigation()->menu()->setMinDepth(null)->setMaxDepth(null)->setOnlyActiveBranch(false)->setRenderInvisible(true);
<块引用>
创建面包屑
echo $this->navigation()->面包屑()->setLinkLast(true)->setMaxDepth(1)->setSeparator(' ▶' . PHP_EOL);
<块引用>
我希望它可以帮助您节省时间
I want ask you about this problem. What do i need to create Dynamic Menu with ZendNavigationNavigation?
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 configautoload av.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 ZendNavigationNavigation;
$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 endDbAdapterAdapterAwareInterface) {
$instance->setDbAdapter($sm->get('ZendDbAdapterAdapter'));
}
}
),
'invokables' => array(
'menu' => 'ApplicationModelMenuTable',
),
'factories' => array(
'Navigation' => 'ApplicationNavigationYourNavigationFactory'
)
);
}
in src/navigation folder in your module create YourNavigation.php and YourNavigationFactory.php
namespace ApplicationNavigation;
use ZendServiceManagerServiceLocatorInterface;
use ZendNavigationServiceDefaultNavigationFactory;
use AdminModelEntityTablepages;
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 ExceptionInvalidArgumentException('Could not find navigation configuration key');
}
if (!isset($configuration['navigation'][$this->getName()])) {
throw new ExceptionInvalidArgumentException(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 ApplicationNavigation;
use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;
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
相关文章