Gernerate custom urls within Magento

后端 未结 3 804
鱼传尺愫
鱼传尺愫 2020-12-13 22:21

I am currently looking at trying to generate custom urls/routing using magento, currently I have set a default route in config.xml within the local module.

&         


        
3条回答
  •  不知归路
    2020-12-13 22:59

    Code below is untested, but should work

    If you don't want to define custom rewrite for each protfolio item, just follow these steps:

    1. Write your custom router class extended from Mage_Core_Controller_Varien_Router_Standard and implement match method:

      public function match(Zend_Controller_Request_Http $request)
      {
          $path = explode('/', trim($request->getPathInfo(), '/'));
          // If path doesn't match your module requirements
          if (count($path) > 2 && $path[0] != 'portfolios') {
              return false; 
          }
          // Define initial values for controller initialization
          $module = $path[0];
          $realModule = 'Custom_Portfolios';
          $controller = 'index';
          $action = 'action';
          $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;
      }
      
    2. Define your custom router in config.xml:

      
          
              
                                                 
                      
                          frontend
                          Custom_Portfolios_Controller_Router_Custom
                      
                  
              
          
      
      
    3. Enjoy your custom routing in Magento :)

提交回复
热议问题