开发者

How to get the URL of a page/section in Magento admin

I can't seem to find a definitive answer on this one. How do you get a url to a page in the admin backend?

For example, let's say I have a module that uses a controller with a frontname of mymodule. If I call:

$this->getUrl('mymodule/controller/action')

I get:

http://mydomain.com/index.php/mymodule/controller/action

This doesn't work because it doesn't have the admin frontname included.

I found this thread that mentions using this:

Mage::helper("adminhtml")->getUrl();

However that doesn't append the admin frontname to the URL either...so what's the point of passing it through the adminhtml helper? The thread also says you can use this to get the admin frontname:

Mage::getConfig()->getNode('admin/routers/adminhtml/args/frontName');

So am I supposed to build the correct URL using these 2 methods or is there an easier way to accomplish this? I would figure there has to be an easy way, but I just can't figure it out.

Thanks :开发者_如何转开发)


You are indeed intended to get the URL via $this->getUrl('mymodule/controller/action'). However, the confusion is stemming a mis-understanding of the routing system. The admin tag specifies to use the admin router, and the mymodule specifies the front name that is used in the URL. Basically you are creating your own front name. To utilize the admin front name, you need to use a controller rewrite in order to accomplish it. Something like the following:

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

What this does is configure the routing system to look at your controller for an appropriate action, but only if it doesn't find one on a Mage_Adminhtml controller.

Do note that when using this rewrite, you would need to use 'adminhtml' instead of 'mymodule' in the call to getURL since your URLs would be falling under the front name setup by adminhtml.

What you figured out is the correct solution to getting your own front name working, and will work just fine, as long as you don't care about using the admin front name. :)


I think I figured this one out, but please comment or add your own answer if I'm mistaken. My first issue was that my config.xml was configured incorrectly. I had <frontend> instead of <admin> under the <routers> element. I also had <use>standard</use> instead of <use>admin</use>. Copy-Paste mistake ;)

However, that still didn't seem to change the URL being returned, but that doesn't seem to matter. It's now correctly hitting my controller, even though the url I'm redirecting to doesn't contain the admin frontname. So I'm a bit confused about that, but regardless, it's working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜