hammygoonan
hammygoonan

Reputation: 2225

Magento Custom Router loading controller but nothing else

I'm trying to get some custom routing going on in Magento using the following code (which I've only slightly modified from here https://stackoverflow.com/a/4158571/1069232):

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;
}

}

The result is a page where the template has loaded but with no content. If I comment out the $this->loadLayout() / $this->renderLayout() in my controller I can print to screen. But when I try and load a Template and/or Block it breaks somewhere.

home.html also loads fine (as the method returns false if the path is home.html).

Any assistance would be greatly appreciated.

Upvotes: 0

Views: 1036

Answers (1)

albertoj
albertoj

Reputation: 36

I was implementing something similar to this and came across the same problem(That makes sense, because I copypasted your code)

before $request->setDispatched(true); I added $request->setRouteName('brands'); (brands is the frontname of my module). And It worked.Don't know if It'll work for you, but definetely there was something missing so that magento didn't know what layout to apply, because I could tell that teh controller was being reached.

Upvotes: 2

Related Questions