我想问你这个问题。我需要什么来创建带有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 中,页面是从配置中获取的。现在我创建 \config\autoloadav.global.php 在这里创建页面数组。但是我需要在方法中做页面数组并将其发送到导航助手中,但我不知道如何((
我尝试在我的控制器中执行此操作:
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);
然后这个观点:
$this->Menu()->menu();
但是我有很多错误...
我想你明白我的问题。请帮忙。对不起我的英语。
按照这些简单的步骤动态创建多级菜单 步骤 1:创建一个部分menu.phtml文件并将其保存在布局文件夹或 模块,例如应用程序/视图/布局/菜单.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.php
public function getServiceConfig()
{
return array(
'initializers' => array(
function ($instance, $sm) {
if ($instance instanceof ZendDbAdapterAdapterAwareInterface) {
$instance->setDbAdapter($sm->get('ZendDbAdapterAdapter'));
}
}
),
'invokables' => array(
'menu' => 'ApplicationModelMenuTable',
),
'factories' => array(
'Navigation' => 'ApplicationNavigationYourNavigationFactory'
)
);
}
在模块的 src/navigation 文件夹中创建您的导航.php 您的导航工厂.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;
}
}
您的导航工厂.php
namespace ApplicationNavigation;
use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;
class YourNavigationFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$navigation = new MyNavigation();
return $navigation->createService($serviceLocator);
}
}
在你的布局中.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);
我希望它能帮助您节省时间
对于某些背景,您可能会在另一个关于ZendNavigation
的类似问题上阅读此答案。关键是你想要 MVC 页面和 Zend Framework 2 中的 MVC 页面需要一种方法来组装 url 并找到我们的 url 是否处于活动状态。
每个 MVC 页面都有一个路由名称。路由堆栈路由请求并获取路由匹配项。您必须将此路由匹配项注入到导航中,以便每个页面都可以根据匹配的路由检查自己的路由。
与 url 程序集类似。如果要将路由名称转换为 url,则需要路由堆栈("路由器")。将路由器也注入您的应用程序中,您就可以组装了。
总之:
use ZendNavigationServiceConstructedNavigationFactory;
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 ZendNavigationPageMvc(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 ZendNavigationPageMvc(array(
'pages'=>
array(
'album' => array(
'label' => 'Album3',
'controller' => 'album',
'action' => 'edit',
'params' => array('id'=>2),
'route' => 'album/default',
)
)
));
$navigation = new ZendNavigationNavigation();
$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() ?>
Eremite 的答案并没有错。我已经测试了这里给出的所有建议,实际上当默认路由有几个child_routes标记为默认值时,菜单不会正确标记活动标志。因此,需要将匹配的路由作为参数传递。但也许我做错了什么。
干杯