带有 ZendNavigationNavigation 的 ZF2 动态菜单 [英] ZF2 Dynamic Menu with ZendNavigationNavigation

查看:30
本文介绍了带有 ZendNavigationNavigation 的 ZF2 动态菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你这个问题.使用 ZendNavigationNavigation 创建动态菜单需要什么?

在 ZF1 中我是这样制作的:

$container = new Zend_Navigation();$pages = 数组(大批('标签' =>'保存','动作' =>'保存',),大批('标签' =>'删除','动作' =>'删除',),);//添加两个页面$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

 

<?php endforeach;?></div><!-- 结束下拉容器--><?php endforeach;?>

<块引用>

module.php 中的第 2 步

公共函数 getServiceConfig(){返回数组('初始值设定项' =>大批(函数($instance,$sm){if ($instance instanceof endDbAdapterAdapterAwareInterface) {$instance->setDbAdapter($sm->get('ZendDbAdapterAdapter'));}}),'invokable' =>大批('菜单' =>'应用程序模型菜单表',),'工厂' =>大批('导航' =>'应用程序导航你的导航工厂'));}

<块引用>

在模块的 src/navigation 文件夹中创建 YourNavigation.php 和YourNavigationFactory.php

命名空间应用导航;使用 ZendServiceManagerServiceLocatorInterface;使用 ZendNavigationServiceDefaultNavigationFactory;使用 AdminModelEntityTablepages;类 YourNavigation 扩展了 DefaultNavigationFactory{受保护的函数 getPages(ServiceLocatorInterface $serviceLocator){if (null === $this->pages) {$fetchMenu = $serviceLocator->get('menu')->fetchAll();$configuration['导航'][$this->getName()] = array();foreach($fetchMenu as $key=>$row){$subMenu = $serviceLocator->get('menu')->fetchAllSubMenus($row['id']);如果($子菜单){$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','类别'=>$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'] = '签证';if ($value['path'] ==$row['path']){$subpage['params'] = array('action'=>'index','类别'=>$row['path'],'sub_category'=>$value1['path']);}elseif($row['id'] ==76){$subpage['params'] = array('action'=>'index','category'=>$value['path'],'sub_category'=>$value1['path']);}别的{$subpage['params'] = array('action'=>'index','类别'=>$row['path'],'sub_category'=>$value['path'],'id'=>$value1['path']);}}$group[] =$subpage;}$page['pages'] =$group;$pages[] =$page;}}}}$configuration['navigation'][$this->getName()][$row['name']] = array('标签' =>$row['name'],'路线' =>'签证','参数' =>大批('动作' =>'指数','类别' =>$row['path'],),'页面' =>$页,);}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();返回 $navigation->createService($serviceLocator);}}

<块引用>

在你的 layout.phtml

 <?php echo $this->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

这篇关于带有 ZendNavigationNavigation 的 ZF2 动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆