2011-01-27 60 views
0

我有一個項目,它使用基於自定義路由的模塊目錄而不是默認路由。系統加載每個模塊的routes.ini文件並將這些路由添加到Zend_Controller_Router_Rewrite路由器中。Zend Framework - Router_Rewrite鏈接?

protected function _initRoutes() { 
     $this->bootstrap('FrontController'); 
     $front = $this->getResource('FrontController'); 

     $routes = Module_Loader::getInstance()->getRoutes(); 

     $front->setRouter($routes); 
     $front->getRouter()->removeDefaultRoutes(); 
    } 

我想在這裏實現的是將語言代碼預先放入uri中,但與Router_Rewrite鏈接是不可能的。 我有一個路線:

[routes] 
;Index page 
routes.core_index_index.type = "Zend_Controller_Router_Route_Static" 
routes.core_index_index.route = "/" 
routes.core_index_index.defaults.module = "core" 
routes.core_index_index.defaults.controller = "index" 
routes.core_index_index.defaults.action = "index" 
routes.core_index_index.defaults.frontend = "true" 
routes.core_index_index.defaults.langKey = "route_index_page_description" 
routes.core_index_index.defaults.localization.enable = "true" 

基本上我想要的網址看起來像:http://myhost.com/en/http://myhost.com/它應該直接到核心模塊中的網頁控制器的這一指標作用。

我可以將這個附加到上面的路由規則中作爲「/([en /] *)」,但我不希望模塊關心如何處理這樣的系統相關的功能。

這可能嗎?

謝謝。

回答

1

您可以使用下面的配置。它應該適用於所有控制器:

[routes] 
;Index page 
routes.core_index_index.route = ":language/:@controller/:@action/*" 
routes.core_index_index.defaults.controller = "index" 
routes.core_index_index.defaults.action = "index" 
routes.core_index_index.reqs.language= "[a-z]{2}" 
routes.core_index_index.defaults.frontend = "true" 
routes.core_index_index.defaults.langKey = "route_index_page_description" 
routes.core_index_index.defaults.localization.enable = "true" 

您需要在每次使用此路由時設置參數'語言'。 爲方便起見,您可以設置默認值這樣的「語言」:

Zend_Controller_Front::getInstance()->getRouter()->setGlobalParam('language', 'en'); 
+0

感謝Yarson,這一個看起來像它會工作,但我不認爲它會做的伎倆。因爲我正在尋找一種解決方案來防止在系統中添加語言到每條路線。 setGlobalParam沒問題,但我的路線相當多。是否有任何簡單的全球解決方案的所有路線? – Maverick 2011-01-27 13:37:23