2009-08-12 68 views
4

我在當前的Zend Framework應用程序中遇到問題。Zend_Controller_Router_Exception:未指定「xyz」

在我引導我註冊這些路線:

protected function _initRouter() 
{ 
    $this->bootstrap("FrontController"); 
    $frontController = $this->getResource("FrontController"); 

    $route = new Zend_Controller_Router_Route(
     ":module/:id", 
     array(
      "controller" => "index", 
      "action" => "index" 
      ), 
     array("id" => "\d+") 
     ); 
    $frontController->getRouter()->addRoute('shortcutOne', $route); 

    $route = new Zend_Controller_Router_Route(
     ":module/:controller/:id", 
     array("action" => "index"), 
     array("id" => "\d+") 
     ); 
    $frontController->getRouter()->addRoute('shortcutTwo', $route); 

    $route = new Zend_Controller_Router_Route(
     ":module/:controller/:action/:id", 
     null, 
     array("id" => "\d+", "action" => "\w+") 
     ); 
    $frontController->getRouter()->addRoute('shortcutThree', $route); 
} 

現在以後我加Zend_Navigation到我的項目。我有幾個模塊,在該模塊引導註冊導航元素:

<?php 

class Contact_Bootstrap extends Zend_Application_Module_Bootstrap 
{ 
    protected function _initNavigation() 
    { 
     $layout = $this->getApplication()->getResource("layout"); 
     $view = $layout->getView(); 

     $config = new Zend_Config_Xml(dirname(__FILE__)."/config/navigation.xml", "nav"); 

     $view->navigation()->addPage($config); 
    } 
} 

當我在瀏覽器中打開應用程序,一切工作正常。這意味着我可以看到導航並單擊其鏈接來查看指定的頁面。但是,當我打了使用頁面:模塊/:動作/:身份證路線,導航助手拋出一個異常Zend_Controller_Router_Route中:

Fatal error: Zend_Controller_Router_Exception: id is not specified in C:\Entwicklung\kt\trunk\src\library\Zend\View\Helper\Navigation\HelperAbstract.php on line 519

難道有人對你遇到這個錯誤嗎?如果你能幫助我或給我建議,那將是非常棒的。另外,如果您需要更多關於我的設置等信息,請告訴我。

謝謝!

回答

6

我自己找到了解決方案。

這個問題是由於Zend_Navigation元素的一個不太有益的行爲造成的。在他們的getHref()方法中,他們使用URL助手。這個幫助程序創建導航條目必須鏈接到的URL,併爲導航元素(模塊,控制器,操作等)指定參數

現在,問題是,如果您創建了自定義路由在你的引導,就像我一樣,例如

":module/:controller/:id" 

您遇到的問題是,在使用這條路線時,該URL助手使用的路由生成的鏈接導航條目。但是,我沒有傳遞額外的「id」參數,所以我得到了異常。

因此,一種解決方案是爲每個設置爲「default」的Zend_Navigation_Page_Mvc實例「route」傳遞一個附加參數。

另一種解決方案,這是我沒有嘗試呢,就是讓新的自定義路線只是重新映射到自己的缺省路由,如:

":module/:controller/:action/id/$id" 

但我不知道你是否有Zend Framework能夠做到這一點。

2

在這條航線 「:模塊/:控制器/:ID」 你需要簡單地定義默認值:ID

例子:

<content type="Zend_Controller_Router_Route"> 
    <route>content/:engine</route> 
    <defaults module="content" controller="engine" action="index" engine="" /> 
</content> 

對不起,我的英語:)

問候。

2

我剛剛遇到類似於您的問題,但爲此找到了不同的解決方法。我只是爲了提供信息而在此分享。

就像你一樣,我的問題似乎是我的導航是使用上次使用的路線建立的。這意味着如果我的自定義路由之一已經被使用,我會得到一堆異常,抱怨這個給定路由的一些自定義參數沒有被定義。

我的路線「自定義參數」的大部分值在我的路線定義中默認爲null。我發現如果我將默認值更改爲「',那麼異常實際上會消失。我無法真正理解這一點,所以我開始使用google搜索,偶然發現了你的帖子,這讓我走上了正確的道路 - 我想。

我的導航元素存儲在數據庫中,所以我根據請求生成所有的Zend_Navigation_Page元素,並將容器傳遞給我的視圖幫助器(我做序列化我的容器對象使其可緩存,但現在不相關)。我並沒有真正發現將參數的默認值改爲「'看起來像一個」好「的做法,所以你的帖子暗示了我試圖改變我存儲在我的數據庫中的所有頁面元素以使用路線命名爲「默認」,除非他們明確要使用自定義路由併爲其定義參數。顯式地定義我的導航點以使用默認路由似乎也可以做到這一點,對我而言,它似乎是一個更簡潔的解決方法。

感謝您的提示是什麼原因導致這個錯誤 - 與例外,我真的沒有哪裏開始:)

+0

嗨基督徒! 我很高興我可以幫助您解決我的問題。 – 2010-02-17 11:30:51

+0

通過一些工作,這個答案歸結爲:給你的導航頁面一個明確的路線。否則,它將遍歷所有路線/選擇第一個,試圖強制導航URL進入該路線。如果該路線具有必需/非默認參數,那麼它將給出該例外,而不是智能地實現路線不適當。 (讓我走上正確的道路+1) – Zimzat 2011-10-01 21:50:05

4
$route = new Zend_Controller_Router_Route( 
    "info/:id", 
    array( 
     "controller" => "index", 
     "action" => "index" 
    ), 
    array("id" => "\d+") 
); 

改爲

$route = new Zend_Controller_Router_Route( 
    "info/:id", 
    array( 
     "controller" => "index", 
     "action" => "index", 
     "id" => "default" 
    ) 
); 

爲我工作的線索,但可能不是最好的方式去

+0

也適用於我 – 2012-11-06 15:40:12

0

我在這裏看到兩種解決方案:

  1. 您可以在_initRouter()中爲您的路由參數設置默認值,但它可能不是您想要的(例如,在這種情況下,可以在不需要時使用此路由)

  2. 您可以在navigation.xml(params屬性)中指定路由參數。如果您不想將它們設置爲固定的,則需要編寫用於生成導航xml的PHP代碼。

3

我都面臨着同樣的問題,在這裏爲我工作的解決方案: 我添加的路由參數,我在我的navigation.xml文件的所有鏈接如下

<?xml version="1.0" encoding="UTF-8"?> 
    <navigation> 
     <mainmenu> 
     <home> 
       <label>topmenu:homepage</label> 
       ===> <route>default</route> <=== 
       <module>default</module> 
       <controller>index</controller> 
       <action>index</action> 
     </home> 
    </mainmenu> 
</navigation>