Gernerate custom urls within Magento

本小妞迷上赌 提交于 2019-11-28 18:57:23

The way to do this is with an URL rewrite. In fact, the suffix config you found is probably used by Mage_Catalog to create it's sets of rewrites. I'm approaching this particular feature for the first time so this snippet should be taken with a pinch of salt...

// Creating a rewrite
/* @var $rewrite Mage_Core_Model_Url_Rewrite */
$rewrite = Mage::getModel('core/url_rewrite');
$rewrite->setStoreId($store_id)
        ->setIdPath('portfolios/'.$url_key)
        ->setRequestPath('portfolios/'.$url_key.'.html')
        ->setTargetPath('portfolios/index/action/id/'.$url_key)
        ->setIsSystem(true)
        ->save();

A new rewrite is needed for each possible path.

Edit; I've added a setIdPath because it might be necessary.

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:

    <stores>
        <default>
            <web>
                <routers>                               
                    <your_custom>
                        <area>frontend</area>
                        <class>Custom_Portfolios_Controller_Router_Custom</class>
                    </your_custom>
                </routers>
            </web>
        </default>
    </stores>
    
  3. Enjoy your custom routing in Magento :)

The easiest method (when you do not need to auto-generate many urls) is to use built-in Url Rewrites module. Go to admin backend -> Catalog -> Url Rewrite management and setup any url rewrite you like.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!