How to use getUrl() in Magento to refer to another module?

silex picture silex · Sep 5, 2011 · Viewed 12.1k times · Source

My module in Magento adminpanel has URL like as http://example.com/index.php/mymodule/... and contains custom grid with the orders. I want to redirect user to the standard "Order view" page when he clicks on a grid row.

public function getRowUrl($row)
{
    if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
        return $this->getUrl('sales_order/view', array('order_id' => $row->getId()));
    }
    return false;
}

But this URL points to http://example.com/index.php/sales_order/view/... instead of http://example.com/index.php/admin/sales_order/view/... Any suggestion?

UPD. config.xml:

<admin>
    <routers>
        <mymodule>
            <use>admin</use>
            <args>
                <module>Foo_Mymodule</module>
                <frontName>mymodule</frontName>
            </args>
        </mymodule>
    </routers>
</admin>

Answer

clockworkgeek picture clockworkgeek · Sep 5, 2011

Quite simply you need to replace sales_order/view with */sales_order/view. The * means use the current router which in the admin is adminhtml.

Edit
To explain in more detail put this in your config,

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <mymodule after="Mage_Adminhtml">Foo_Mymodule_Adminhtml</mymodule>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

Now the value */mymodule/index will generate an URL http://example.com/index.php/admin/mymodule/index which in turn will load the file Foo/Mymodule/controllers/Adminhtml/MymoduleController.php and try to find the method Foo_Mymodule_Adminhtml_MymoduleController::indexAction(). If the method exists it is run, otherwise the admin router takes over and shows a 404 or redirects to the dashboard.