2011-02-08 78 views
2

Iam開發一個Zend RestController,我必須生成響應爲XML格式。 我添加上下文切換,因此我可以看到類似的URL我的XML輸出:自定義ZendRestController URL

的http://本地主機/應用/公/ API /按鍵/ 123456 /格式/ XML/

(其中「API」 是控制器,關鍵= 123456,通過獲得通過)

但我想要的是,URL應該是這樣的:

的http://本地主機/應用/公/ API/V1/abc.xml/

(目前,請留下「鑰匙」參數,因爲以後我會通過捲髮發送)

我該如何做到這一點?我需要調整網址的東西嗎?蔭主要關注的改變「格式/ XML」到「abc.xml」

+1

`http:// localhost/app/public/api/key/123456/format/xml /`---這個URL不遵循REST思想。更合適的是`http:// localhost/app/public/api/keys/123456.xml` – zerkms 2011-02-08 08:01:41

+0

我嘗試過url:localhost/app/public/api/keys/123456.xml,我不得不爆炸「鍵「來獲取數字(用於我的查詢中)。但現在,我無法獲得輸出格式爲xml。除非我在url結尾指定「/ format/xml」,否則iam不會獲得xml輸出。有什麼辦法嗎? – 2011-02-08 09:03:30

回答

2

我的路線設置在我的應用程序引導爲:

/* 
* ReWrite Rules and Routes 
*/ 
protected function _initRoutes() { 
    $router = Zend_Controller_Front::getInstance()->getRouter(); 
    // New Route 
    $router->addRoute('api', new Zend_Controller_Router_Route('/api/:key.xml', array('module'=>'index', 'controller' => 'api', 'action' => 'index', 'key'=>''))); 
} 

然後使用url幫手爲:

<a href="<?= $this->url(array('key'=>123456), 'api') ?>">API Call</a>