本文介绍了ZF2动态菜单,带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 $我认为你明白我的问题。
请帮忙。
对我的英文很抱歉

解决方案



  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'

);
}





 命名空间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;
}
}





 命名空间Application\Navigation; 

使用Zend\ServiceManager\FactoryInterface;
使用Zend\ServiceManager\ServiceLocatorInterface;

class YourNavigationFactory实现FactoryInterface
{
public function createService(ServiceLocatorInterface $ serviceLocator)
{
$ navigation = new MyNavigation();
return $ navigation-> createService($ serviceLocator);
}
}





 <?php echo $ this-> navigation('navigation' >菜单() - > setPartial( '菜单') - >渲染(); ?> 




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.

解决方案
 <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>
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'
                )
        );
    }
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;
    }
}
 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);
            }
        }
 <?php  echo $this->navigation('navigation')->menu()->setPartial('menu')->render();  ?>
    $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);
echo $this->navigation()
                  ->breadcrumbs()
                  ->setLinkLast(true)
                  ->setMaxDepth(1)
                  ->setSeparator(' ▶' . PHP_EOL);

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

08-31 04:07