2012-11-22 77 views
1

我一直在尋找展現在layout.phtml文件中的導航菜單:Zend框架2導航的異常

<?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 /導航實例:

'service_manager' => array(
     'factories' => array(
      'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', 
      'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory', 
     ), 
    ), 

,但我得到反覆此異常:

Fatal error: 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 line 471

我試圖解決加入這行來的菜單聲明的問題:

   'pages' => array(
        'route' => '/place', 
        'country' => array(
         'label' => 'Countries', 
         'route' => '/place/country', 
        ), 

然而,在這種情況下,我得到另一個不同的異常:

Fatal error: Uncaught exception 'Zend\Navigation\Exception\InvalidArgumentException' with message 'Invalid argument: $page must be an instance of Zend\Navigation\Page\AbstractPage or Traversable, or an array' in C:\wamp\www\bsol\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 733

更新:按照Jurian Sluiman的建議,

這一定是我的路由器配置(模塊\地點\ CONFIG \ module.config.php),如果我有一個所謂的「地方」模塊,用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', 
        ), 
       ), 
      ), 

     ), 
), 

而且這一定是我的導航配置(其實我這個位於一個在autoloadable文件/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', 
        ), 
       ), 
      ), 
     ), 
    ), 
); 

...而現在它的工作原理!

回答

5

通常,路線起點不是//是路由名稱的分隔符,因此當組裝/place路由時,您有一個父節點"",然後是子節點"place"。第一個是空的,給你這個錯誤。

也許你將路線名稱與路線本身混淆了。您必須提供路線的名稱,因此路線本身(網址)將被返回。所以,想象一下你有這條路線的配置:

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

在這種情況下,你的路線place而不是/place。如果你有一個不同的路由配置,那麼上面的那個,把路由配置在你的問題,所以我可以更新我的答案。

+0

是的。你是對的。我在寫模塊的路由配置時犯了一些錯誤。 –

+0

Ahah,這個答案也幫助了我,當我輸入路線時,它裏面有大括號可以打破所有的錯誤,但是錯誤信息與他們不是「翻譯者」有關: '致命錯誤:未捕獲的異常'Zend \ Mvc \ Router \ Exception \ RuntimeException'消息中'沒有提供翻譯器',這讓我很吃驚。 – Kzqai