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

偶尔善良 提交于 2019-12-01 06:25:11

问题


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 :)


回答1:


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. :)




回答2:


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.



来源:https://stackoverflow.com/questions/6877683/how-to-get-the-url-of-a-page-section-in-magento-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!