Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

php - Gernerate custom urls within Magento

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.

<frontend>
 <routers>
         <portfolios>
             <use>standard</use>
             <args>
                 <module>Custom_Portfolios</module>
                 <frontName>portfolios</frontName>
             </args>
         </portfolios>
     </routers>
     <default>
         <router>portfolios</router>
     </default>
 </frontend>

This currently works with the url path of /portfolios/index/action/custom-string which is the magento default route. What I am trying to achieve is to have /portfolios/custom-string.html I have attempted to use a mod_rewrite rule with no success, I have found some references in relation to utilising a custom suffix of .html which I have added to the same config.xml file.

<default><portfolios><seo><portfolios_url_suffix>.html</portfolios_url_suffix></seo></portfolios></default>

I have looked at the alan storm docs in relation to routing and found it relevent to the default routing paths only or the information is a little out-dated.

Do you know the best method to control the routing within magento with possibly an easy to follow and relevent tutorial? if so please share :D many

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...