我試圖從隨機URL(我的應用程序的一部分)獲取路由信息。從Zend_Controller_Request獲取路由信息
我試圖用URL實例化一個Zend_Controller_Request_Http,但它沒有自動填充控制器,模塊,動作域。
我想它應該以某種方式鏈接到路線信息,但不能如何連接這個。
任何線索?
我試圖從隨機URL(我的應用程序的一部分)獲取路由信息。從Zend_Controller_Request獲取路由信息
我試圖用URL實例化一個Zend_Controller_Request_Http,但它沒有自動填充控制器,模塊,動作域。
我想它應該以某種方式鏈接到路線信息,但不能如何連接這個。
任何線索?
它沒有直接關聯。路由器會調用它的route
方法來傳遞請求作爲它的參數。然後以相反的順序循環所有註冊的路由,將請求作爲參數調用路由的match
方法 - 如果匹配,則在請求返回之前設置參數。
問題是你不能直接調用Zend_Controller_Router_Rewrite :: route而不修改當前的請求週期,所以你必須依賴一些「fudgery」或在你自己的ruter sbuclass或其他方法中重現此方法中的邏輯。捏造的
例子:
// assume $router is your router instance, $request is the REAL request.
$testRequest = new Zend_Controller_Request_Http($url);
// need to use a try because if the route doesnt match youve got an exception coming
try {
$router->route($testRequest);
} catch(Zend_Controller_Router_Exception $e) {
$testRequest = false;
}
// revert back to the real current route which was modified during the previous call
$router->route($request);
if(false !== $testRequest) {
// consume what you need form testRequest as you normally would
print_r($testRequest->getParams());
}
我遇到了問題,這倒行後,我開始進入更復雜的要求lifecyles。我不記得爲什麼,但我記得我的解決辦法是子類中的路由器和廣告route
,看上去像這樣的方法:
public function parseRoute(Zend_Controller_Request_Abstract $request)
{
$preservedRoute = $this->_currentRoute;
try {
$router->route($request);
$this->_currentRoute = $preservedRoute;
} catch(Zend_Controller_Router_Exception $e) {
$this->_currentRoute = $preservedRoute;
return false;
}
return $request;
}
也請記住,這是所有的記憶,這是1.6或1.7不是當前版本所以YMMV。希望有所幫助。
有想法,謝謝你的提示。由於我的特定應用沒有複雜的路由,所以我想我會通過我的方式進行軟化。 – 2010-08-11 07:15:53