2012-02-02 51 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