我正在寻找在 layout.phtml 文件中显示导航菜单:
<?php echo $this->navigation('navigation')->menu(); ?>
因此,我将这些行添加到 /module/Application/config/module.config.php 以便根据我的应用程序路由声明菜单的结构:
'navigation' => array(
'default' => array(
'place' => array(
'label' => 'Places',
'route' => '/place',
'pages' => array(
'country' => array(
'label' => 'Countries',
'route' => '/place/country',
),
'state' => array(
'label' => 'States',
'route' => '/place/state',
),
'city' => array(
'label' => 'Cities',
'route' => '/place/city',
),
),
),
),
),
然后我为 Zend/Navigation 实例编写了一个加载器:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
但我反复得到这个例外:
致命错误:Zend\Mvc\Router\Exception\RuntimeException: Route with name "" not found in C:\wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\View\Helper\Navigation\AbstractHelper.php on第 471 行
我试图解决将这一行添加到菜单声明的问题:
'pages' => array(
'route' => '/place',
'country' => array(
'label' => 'Countries',
'route' => '/place/country',
),
然而,在那种情况下,我得到另一个不同的例外:
致命错误:未捕获的异常 'Zend\Navigation\Exception\InvalidArgumentException' 带有消息'无效参数:$page 必须是 Zend\Navigation\Page\AbstractPage 或 Traversable 的实例,或 C:\wamp\www\bsol 中的数组' \vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php 在第 733 行
更新:根据 Jurian Sluiman 的建议,
这必须是我的路由器配置(module\Place\config\module.config.php),如果我有一个名为“Place”的模块,带有 3 个控制器(城市、国家和州):
'router' => array(
'routes' => array(
'city' => array(
'type' => 'segment',
'options' => array(
'route' => '/city[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\City',
'action' => 'index',
),
),
),
'country' => array(
'type' => 'segment',
'options' => array(
'route' => '/country[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\Country',
'action' => 'index',
),
),
),
'state' => array(
'type' => 'segment',
'options' => array(
'route' => '/state[/:action][/:oid]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Place\Controller\State',
'action' => 'index',
),
),
),
),
),
这一定是我的导航配置(实际上我在 /config/autoload/menu.global.php 的可自动加载文件中找到了这个):
return array(
'navigation' => array(
'default' => array(
'place' => array(
'label' => 'Places',
'route' => 'country',
'pages' => array(
'country' => array(
'label' => 'Countries',
'route' => 'country',
),
'state' => array(
'label' => 'States',
'route' => 'state',
),
'city' => array(
'label' => 'Cities',
'route' => 'city',
),
),
),
),
),
);
...现在它起作用了!