2011-11-02 49 views
0

我想在route.ini文件中設置Zend Framework(版本1.11.11)中的路由,這將允許匹配以下url:動作參數路由不工作在Zend框架routes.ini

my.domain.com/shop/add/123 

ShopControlleraddAction。但是,由於某種原因,參數(最後的數字)沒有被我的動作所識別。我得到的PHP錯誤是

Warning: Missing argument 1 for ShopController::addAction(), called in... 

我知道我可以在引導使用PHP代碼中設置的,但我想知道如何做到這一點的安裝類型在一個.ini文件,我很難找到解釋這一點的資源。我還應該指出,我在我的項目中使用了模塊。我想出了利用這裏找到各種片段,並有在線如下:

的application/config/routes.ini:

[routes] 
routes.shop.route = "shop/add/:productid/*" 
routes.shop.defaults.controller = shop 
routes.shop.defaults.action = add 
routes.shop.defaults.productid = 0 
routes.shop.reqs.productid = \d+ 

bootstrap.php中:

... 
protected function _initRoutes() 
    { 
     $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'routes'); 
     $router = Zend_Controller_Front::getInstance()->getRouter(); 
     $router->addConfig($config, 'routes'); 
    } 
... 

ShopController .php

<?php 

class ShopController extends Egil_Controllers_BaseController 
{ 

    public function indexAction() 
    { 
     // action body 
    } 

    public function addAction($id) 
    { 
     echo "the id: ".$id; 
    } 

} 

任何建議,爲什麼這不工作?我有一種感覺,我錯過了一些關於通過.ini文件在Zend中路由的基本知識。

回答

2

顯然我在Zend比我想象的更生鏽。發佈後幾分鐘,我意識到我試圖在控制器中以錯誤的方式訪問參數。它不應該是對的addAction一個參數,而不是我應該通過函數內部的請求對象訪問:

正確的addAction在ShopController:

public function addAction() 
{ 
    $id = $this->_request->getParam('productid'); 
    echo "the id: ".$id; 
} 

我也意識到我可以簡化我的路線設置頗有幾分在這種情況下:

[routes] 
routes.shop.route = "shop/:action/:productid" 
routes.shop.defaults.controller = shop 
routes.shop.defaults.action = index