我要问你这个问题。 我需要什么来创建动态菜单与Zend \导航\导航?
在ZF1我犯了这样的:
$container = new Zend_Navigation();
$pages = array(
array(
'label' => 'Save',
'action' => 'save',
),
array(
'label' => 'Delete',
'action' => 'delete',
),
);
// add two pages
$container->addPages($pages);
然后在视图:
$this->navigation()->menu();
但在ZF2页是从配置服用。 现在,我创建\设置\自动加载\ nav.global.php这里创建页面阵列。 但我需要做的页面阵列中的方法,并将其发送到导航帮助,但二不知道如何((
我试图做到这一点在我的控制器:
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);
然后在此视图中:
$this->Menu()->menu();
但我有很多的错误......
我想你明白我的问题。 请帮忙。 对不起我的英语不好。
按照以下简单步骤来创建多级菜单动态步骤1:创建部分menu.phtml文件,并将其保存在布局文件夹或模块,例如,应用/视图/布局/ 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>
步骤2在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'
)
);
}
在你的模块中的src /导航文件夹中创建YourNavigation.php和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);
}
}
在layout.phtml
<?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);
我希望它可以帮助您节省您的时间
对于一些背景,你可能会读这个答案在另一个,但类似的问题有关Zend\Navigation
。 问题的关键是你要MVC页面和Zend框架MVC 2页需要一种方法来组装URL并找到我们如果URL激活。
每一个MVC页面都有一个路由名称。 路线堆栈路由请求,并得到一个路由匹配。 你必须注入这条路线将比赛拖入导航,让每个页面可以核对匹配一个自己的路线。
类似的URL装配。 如果你想的路线名字转换成一个URL,你需要的路线栈(“路由器”)。 注入在应用程序中的路由器也和你能够组装。
简而言之:
use Zend\Navigation\Service\ConstructedNavigationFactory;
class MyController extends AbstractActionController
{
public function indexAction()
{
$config = array(
// your config here
);
$factory = new ConstructedNavigationFactory($config);
$navigation = $factory->createService($this->getServiceLocator());
return new ViewModel(array(
'navigation' => $navigation;
));
}
}
而类似上述的答案,在你的看法:
<?php echo $this->navigation($navigation)->menu()?>
我以前的答案是不正确的。 下面的代码工作。 对于一个页面。 在控制器,编辑动作:
$page = new \Zend\Navigation\Page\Mvc(array(
'route' => 'application/default',
'controller' => 'album',
'action' => 'edit',
'use_route_match' => true,
));
$r = $this->getEvent()->getRouter();
$rm = $this->getEvent()->getRouteMatch();
$page->setRouter($r);
$page->setRouteMatch($rm);
echo $page->isActive() ? 'true' : 'false'; // true
echo $page->getHref(); // /test_app/public/application/album/edit/id1
你需要这样做
在控制器
$pages = new \Zend\Navigation\Page\Mvc(array(
'pages'=>
array(
'album' => array(
'label' => 'Album3',
'controller' => 'album',
'action' => 'edit',
'params' => array('id'=>2),
'route' => 'album/default',
)
)
));
$navigation = new \Zend\Navigation\Navigation();
$serviceLocator = $this->getServiceLocator()->get('Application');
$routeMatch = $serviceLocator->getMvcEvent()->getRouteMatch();
$router = $serviceLocator->getMvcEvent()->getRouter();
$pages->setRouteMatch($routeMatch);
$pages->setDefaultRouter($router);
$navigation->addPage($pages);
鉴于
<?php echo $this->navigation($this->navigation)->menu() ?>
遁世修行的人的答案是不是真的错了。 我已经测试在这里,实际上给出的所有建议时的缺省路由有几个child_routes标记为默认,菜单不正确标记活动标志。 因此,它是需要通过匹配的路由作为参数。 但也许我做错了。
干杯