tags:

views:

49

answers:

1

HI,

I am trying to create a router for my custom module.

I would like to access index controller's hotsale action by url/hotsale. So I create the following router xml in my module's config.xml.

<hotsale>
 <use>standard</use>
 <args>
  <module>Moon_Products</module>
  <frontName>hotsale</frontName>
 </args>
</hotsale>

when I access url/hotsale, it goes to index controller's index action. How do I make it to execute hotsale action?

I tried to add hotsale, but it didn't work.

I took Alan Storm's suggestion and ended with the following code.

public function indexAction()
{
    if($this->getRequest()->getRouteName() == 'hotsale'){

        $this->loadLayout();    

        $block = $this->getLayout()->createBlock(
            'Mage_Core_Block_Template',
            'my_block_name_here',
            array('template' => 'moon/hotsale.phtml')
        );


        $this->getLayout()->getBlock('root')->setTemplate('page/product-without-rightbar.phtml');
        $this->getLayout()->getBlock('content')->append($block);

        $this->renderLayout();

    }
}
+1  A: 

Default frontend/store routing works like this

http://example.com/front-name/controller-name/action-name

So, when you go to

http://example.com/hostsale

You're really saying

http://example.com/hostsale/index/index

The front-name concept is a little abstract, but in practice is ties a URL to a particular module.

So, if have an IndexController.php with a method named hotsaleAction, and you want to execute this method, use a URL in the form

http://example.com/hotsale/index/hotsale 
Alan Storm
@Alan Storm // Thank a lot!! by the way :) I read your blog!!
Moon
@Alan Storm // is there anyway to route /hotitem to hotitemAction directly in xml ?
Moon
Re: overriding the default Magento routing so that /hotitem goes directly to the hotitemAction. Probably, but it's not easy (or worth it, in my opinion). I've never tried it, but my approach would be to change "<use>" to a a custom value, add a custom router to your config, create a router class for that router, and then rewrite the match method. (and I'm not 100% certain that <use/> would let you get away with that). I'd just have your indexAction do something like [return $this->hotsaleAction]. Quicker, easier, and won't confuse the next dev.
Alan Storm
@Alan Storm // Yes, I took your suggestion.
Moon