Magento – How to rewrite url by the custom url in custom module

controllersmagento-1.9routerurl-rewrite

I used this tutorial

http://inchoo.net/magento/custom-router-in-magento/
https://shakyaabiral.wordpress.com/tag/magento-custom-url-rewrite/

but it does not work.
My anchor tag url is 127.0.0.1/testmagento/index.php/testimonial/index/view/testid/11 and when I click my anchor tag match function does not call which is mentioned in Router.php file
I want to convert url from

127.0.0.1/testmagento/index.php/testimonial/index/view/testid/11

to

127.0.0.1/testmagento/index.php/testimonial/testimonialname

Where I should to modify code for achieve this custom url 127.0.0.1/testmagento/index.php/testimonial/testimonialname

this testimonialname(e.g. testimonial1) will fetch from database table

I also put die('match') in match function but match fucntion does not call.

What is my mistake ?

my config.xml

   <stores>
        <default>
            <web>
                <routers>                               
                    <testimonial>
                        <area>frontend</area>
                        <class>Test_Testimonial_Controller_Router</class>
                    </testimonial>
                </routers>
            </web>
        </default>
    </stores>

my Test/Testimonial/Controller/Router.php file

<?php
class Test_Testimonial_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard
{
  public function match(Zend_Controller_Request_Http $request)
    { die('match');
        //checking before even try to find out that current module
        //should use this router
        if (!$this->_beforeModuleMatch()) {
            return false;
        }

        $this->fetchDefault();

        $front = $this->getFront();
        $path = trim($request->getPathInfo(), '/');

        if ($path) {
            $p = explode('/', $path);
        } else {
            $p = explode('/', $this->_getDefaultPath());
        }

        // get module name
        if ($request->getModuleName()) {
            $module = $request->getModuleName();
        } else {
            if (!empty($p[0])) {
                $module = $p[0];
            } else {
                $module = $this->getFront()->getDefault('module');
                $request->setAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS, '');
            }
        }
        if (!$module) {
            if (Mage::app()->getStore()->isAdmin()) {
                $module = 'admin';
            } else {
                return false;
            }
        }

        /**
         * Searching router args by module name from route using it as key
         */
        $modules = $this->getModuleByFrontName($module);

        if ($modules === false) {
            return false;
        }

        //checkings after we foundout that this router should be used for current module
        if (!$this->_afterModuleMatch()) {
            return false;
        }

        /**
         * Going through modules to find appropriate controller
         */
        $found = false;
        foreach ($modules as $realModule) {
            $request->setRouteName($this->getRouteByFrontName($module));

            // get controller name
            if ($request->getControllerName()) {
                $controller = $request->getControllerName();
            } else {
                if (!empty($p[1])) {
                    $controller = $p[1];
                } else {
                    $controller = $front->getDefault('controller');
                    $request->setAlias(
                        Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                        ltrim($request->getOriginalPathInfo(), '/')
                    );
                }
            }

            // get action name
            if (empty($action)) {
                if ($request->getActionName()) {
                    $action = $request->getActionName();
                } else {
                    $action = !empty($p[2]) ? $p[2] : $front->getDefault('action');
                }
            }

            //checking if this place should be secure
            $this->_checkShouldBeSecure($request, '/'.$module.'/'.$controller.'/'.$action);

            $controllerClassName = $this->_validateControllerClassName($realModule, $controller);
            if (!$controllerClassName) {
                continue;
            }

            // instantiate controller class
            $controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $front->getResponse());

            if (!$controllerInstance->hasAction($action)) {
                continue;
            }

            $found = true;
            break;
        }

        /**
         * if we did not found any siutibul
         */
        if (!$found) {
            if ($this->_noRouteShouldBeApplied()) {
                $controller = 'index';
                $action = 'noroute';

                $controllerClassName = $this->_validateControllerClassName($realModule, $controller);
                if (!$controllerClassName) {
                    return false;
                }

                // instantiate controller class
                $controllerInstance = Mage::getControllerInstance($controllerClassName, $request,
                    $front->getResponse());

                if (!$controllerInstance->hasAction($action)) {
                    return false;
                }
            } else {
                return false;
            }
        }

        // set values only after all the checks are done
        $request->setModuleName($module);
        $request->setControllerName($controller);
        $request->setActionName($action);
        $request->setControllerModule($realModule);

        // set parameters from pathinfo
        for ($i = 3, $l = sizeof($p); $i < $l; $i += 2) {
            $request->setParam($p[$i], isset($p[$i+1]) ? urldecode($p[$i+1]) : '');
        }

        // dispatch action
        $request->setDispatched(true);
        $controllerInstance->dispatch($action);

        return true;
    } 
}
?>

Best Answer

Try

<default>
   <web>
     <routers>
        <testimonial_router>
            <area>frontend</area>
            <class>Test_Testimonial_Controller_Router</class>
        </testimonial_router>
    </routers>
   </web>
</default>
<frontend>
    <routers>
        <testimonial>
            <use>testimonial_router</use>
            <args>
                <module>Test_Testimonial</module>
                <frontName>testimonial</frontName>
            </args>
        </testimonial>
    </routers>
</frontend>
Related Topic