我正在寻找在layout.phtml文件中显示导航菜单:
<?php echo $this->navigation('navigation')->menu(); ?>
Run Code Online (Sandbox Code Playgroud)
因此,我将这些行添加到/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',
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
然后我为Zend/Navigation实例编写了一个加载器:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
Run Code Online (Sandbox Code Playgroud)
但我反复得到这个例外:
致命错误:Zend\Mvc\Router\Exception\RuntimeException:在C:\ wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\View\Helper\Navigation\AbstractHelper.php中找不到名称为""的路由第471行
我试图解决将此行添加到菜单声明中的问题:
'pages' => array(
'route' => '/place',
'country' => array(
'label' => 'Countries',
'route' => '/place/country',
),
Run Code Online (Sandbox Code Playgroud)
然而,在那种情况下,我得到另一个不同的例外:
致命错误:未捕获异常'Zend\Navigation\Exception\InvalidArgumentException',消息'无效参数:$ page必须是Zend\Navigation\Page\AbstractPage或Traversable的实例,或C:\ wamp\www\bsol中的数组' 733行的\ vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php
更新:遵循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',
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
这必须是我的导航配置(实际上我将这个位于/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',
),
),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
......现在它有效!
通常路线不是以a开头的/.它/是路由名称的分隔符,因此当/place路由汇编时,您有父级"",然后是子级"place".第一个是空的,给你这个错误.
也许您将路线名称与路线本身混淆了.您必须提供路由的名称,因此将返回路由本身(URL).所以想象你有这条路线配置:
'router' => array(
'routes' => array(
'place' => array(
'type' => 'literal',
'options' => array(
'route' => '/place',
'defaults' => array(
// Some defaults here
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
在这种情况下,你的路线名称是place不/place.如果您有一个不同的路由配置,然后是上面的路由配置,请将路由配置放在您的问题中,以便我可以更新我的答案.
| 归档时间: |
|
| 查看次数: |
8370 次 |
| 最近记录: |