2012-02-02 35 views
0

我試圖讓一些自定義的路由使用下面的代碼(我只稍微從這裏https://stackoverflow.com/a/4158571/1069232修改)在Magento的事情:Magento的自定義路由器負載控制器,但沒有別的

class Company_Modulename_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard { 

public function match(Zend_Controller_Request_Http $request){ 
    $path = explode('/', trim($request->getPathInfo(), '/')); 
    // If path doesn't match your module requirements 
    if ($path[1] == 'home.html' || (count($path) > 2 && $path[0] != 'portfolios')) { 
     return false; 
    } 
    // Define initial values for controller initialization 
    $module = $path[0]; 
    $realModule = 'Company_Modulename'; 
    $controller = 'index'; 
    $action = 'index'; 
    $controllerClassName = $this->_validateControllerClassName(
     $realModule, 
     $controller 
    ); 
    // If controller was not found 
    if (!$controllerClassName) { 
     return false; 
    }    
    // Instantiate controller class 
    $controllerInstance = Mage::getControllerInstance(
     $controllerClassName, 
     $request, 
     $this->getFront()->getResponse() 
    ); 

    // If action is not found 
    if (!$controllerInstance->hasAction($action)) { 
     return false; 
    } 
    // Set request data 
    $request->setModuleName($module); 
    $request->setControllerName($controller); 
    $request->setActionName($action); 
    $request->setControllerModule($realModule); 
    // Set your custom request parameter 
    $request->setParam('url_path', $path[1]); 
    // dispatch action 
    $request->setDispatched(true); 
    $controllerInstance->dispatch($action); 
    // Indicate that our route was dispatched 
    return true; 
} 

}

結果是模板已加載但沒有內容的頁面。如果我在我的控制器中註釋$ this-> loadLayout()/ $ this-> renderLayout(),我可以打印到屏幕上。但是當我嘗試加載模板和/或塊時,它會在某處出現問題。

home.html也加載正常(如果路徑爲home.html,則該方法返回false)。

任何援助將不勝感激。

+0

怎麼樣佈局更新? – Zyava 2012-02-02 19:27:22

回答

2

我正在實施類似這樣的東西,在同樣的問題就來了(這是有道理的,因爲我copypasted你的代碼)

之前$request->setDispatched(true); 我加$request->setRouteName('brands');(品牌是我模塊的frontname)。 它的工作原理。不知道它是否會爲你工作,但明確地說有一些缺失,使得magento不知道應用什麼佈局,因爲我可以告訴控制器正在達到。

+0

太棒了,謝謝albertoj。我一定會嘗試一下。 – hammygoonan 2012-05-02 12:11:44

+0

終於開始測試這個解決方案,它工作得很好!非常感謝! – hammygoonan 2012-05-23 12:37:49