3

I would like to know how to make a custom url rewrite for my module.

ex. http://magento.localhost/retailer/stores/id/2 to magento.localhost/retailer/stores/storename.html

http://magento.localhost/retailer/stores/id/3 to magento.localhost/retailer/stores/storename2.html

  <events>
    <controller_front_init_routers>
        <observers>
            <towersystems_retailer>
                <class>Towersystems_Retailer_Controller_Router</class>
                <method>initControllerRouters</method>
            </towersystems_retailer>
        </observers>
    </controller_front_init_routers>
</events>

Controller Router

class Towersystems_Retailer_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract {

public function initControllerRouters($observer){
    $front = $observer->getEvent()->getFront();
    $front->addRouter('towersystems_retailer', $this);
    return $this;
}

public function match(Zend_Controller_Request_Http $request){
    Mage::log("match", null, "rewrite.txt");
    if (!Mage::isInstalled()) {
        Mage::app()->getFrontController()->getResponse()
            ->setRedirect(Mage::getUrl('install'))
            ->sendResponse();
        exit;
    }

   $urlKey = trim($request->getPathInfo(), '/');
    //split the urlKey by slash
    $parts = explode('/', $urlKey);
    //check if there are at least 3 parts ('client', 'stores' and the actual store name)
    //if there are less than 3 it means it's not the url you are looking for
    if (count($parts) != 3) {
        return false;
    }
    //check if every part matches what it should
    if ($parts[0] == 'retailer' && $parts[1] == 'stores') {
        //get the store name - remove `.html` from the last part of the url
        $storeKey = str_replace('.html', '', $parts[2]);
        //TODO: add here additional check to see if $storeKey is valid (if an entity with your store name exists)
        //tell the request to map to your controller
        $request->setModuleName('retailer')
                ->setControllerName('stores')
                ->setActionName('index')
                ->setParam('id', '2');
        $request->setAlias(
            Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
            $urlKey
        );
        return true;
    }
    return false;
}

}

2 Answers 2

6

you can create a custom router in your module.
For this you will need this in the config.xml of your module inside the global tag

<events>
    <controller_front_init_routers>
        <observers>
            <[vendor]_[module]>
                <class>[Vendor]_[Module]_Controller_Router</class>
                <method>initControllerRouters</method>
            </[vendor]_[module]>
        </observers>
    </controller_front_init_routers>
</events>

Then create the file app/code/local/[Vendor]/[Module]/Controller/Router.php

<?php
class [Vendor]_[Module]_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract 
{
    public function initControllerRouters($observer)
    {
        $front = $observer->getEvent()->getFront();
        $front->addRouter('[vendor]_[module]', $this);
        return $this;
    }    
    public function match(Zend_Controller_Request_Http $request)
    {
        if (!Mage::isInstalled()) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
            exit;
        }
        $urlKey = trim($request->getPathInfo(), '/');
        //split the urlKey by slash
        $parts = explode('/', $urlKey);
        //check if there are at least 3 parts ('client', 'stores' and the actual store name)
        //if there are less than 3 it means it's not the url you are looking for
        if (count($parts) < 3) {
            return false;
        }
        //check if every part matches what it should
        if ($parts[0] == 'client' && $parts[1] == 'stores') {
            //get the store name - remove `.html` from the last part of the url
            $storeKey = str_replace('.html', '', $parts[2]);
            //TODO: add here additional check to see if $storeKey is valid (if an entity with your store name exists)
            //tell the request to map to your controller
            $request->setModuleName('module_front_key_here')
                    ->setControllerName('controller_name_here')
                    ->setActionName('action_name_here')
                    ->setParam('id', 'id of the store with the key $storeKey');
            $request->setAlias(
                Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                $urlKey
            );
            return true;
        }
        return false;
    }
}
11
  • I am actually something like this but I get an error Front controller reached 100 router match iterations
    – tom_cruz
    Dec 3, 2015 at 8:10
  • Use this to debug your router: tutorialmagento.com/…
    – Marius
    Dec 3, 2015 at 8:11
  • also when you said check for actual store name, $parts[2] is just the ID parameter
    – tom_cruz
    Dec 3, 2015 at 9:19
  • $parts[2] should be storename.html
    – Marius
    Dec 3, 2015 at 9:21
  • I've added a snippet of my code above. I am trying to see if it would work I've hard coded the parameter for ID. I think I am not understanding something
    – tom_cruz
    Dec 3, 2015 at 9:31
2

No code is needed for that. Use default settings from admin

Catalog->URL Rewrite Management->Create URL Rewrite: Custom

  1. Use "retailer/stores/id/2" as "Request Path"
  2. Use "retailer/stores/storename.html" as "Target Path" &
  3. set Redirect Yes & save.

Same for other one.

1
  • this is no good if you have 400++ stores
    – tom_cruz
    Dec 3, 2015 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.