Zend Framework 2 Navigation的异常



我想在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' => 'ZendI18nTranslatorTranslatorServiceFactory',
'navigation' => 'ZendNavigationServiceDefaultNavigationFactory',
),
),

但我反复得到这个例外:

致命错误:Zend\Mvc\Router\Exception\RuntimeException:路由在中找不到名称"C: \wamp\www\bsol\vendor\zendframework\zendfframework\library\Zend\View\Helper\Navigation\AbstractHelper.php在线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\zendfframework\library\Zend\ServiceManager\ServiceManager.php733 线上

更新:根据Jurian Sluiman的建议,

如果我有一个名为"Place"的模块,有3个控制器(城市、国家和州),这一定是我的路由器配置(module\Place\config\module.config.php):

'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' => 'PlaceControllerCity',
'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' => 'PlaceControllerCountry',
'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' => 'PlaceControllerState',
'action'     => 'index',
),
),
),
),
),

这一定是我的导航配置(实际上我在/config/autoload/manue.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',
),
),
),
),
),
);

现在它起作用了!。

通常路由不以/开头。/是路由名称的分隔符,因此当组装/place路由时,您会有一个父"",然后是一个子"place"。第一个是空的,这会给你这个错误。

也许您将路线名称与路线本身混淆了。您必须提供路由的名称,因此将返回路由本身(URL)。所以想象一下你有这样的路线配置:

'router' => array(
'routes' => array(
'place' => array(
'type' => 'literal',
'options' => array(
'route'    => '/place',
'defaults' => array(
// Some defaults here
),
),
),
),
),

在这种情况下,名称place,而不是/place。如果你的路由配置与上面的不同,请将路由配置放在你的问题中,这样我就可以更新我的答案。

最新更新