Subesh Pokhrel

Magento Developers Blog

Generating Backend-Admin URL With Key and Parameters in Magento

When you log into adminstrator part of the Magento webshop, and look into the url you may see something like this. [caption id=”attachment_230” align=”alignnone” width=”419” caption=”Admin URL with Keys and Parameters”]Admin URL with Keys and Parameters[/caption]

The part in “RED” are module controller(router)/action. And the part in “GREEN” is what we call as ”Paramerters” and the “BLUE” part is the ”Key”. The Key (which is also a URL parameter) in the URL has been added for security reasons and is checked against the session’s values for every action. If store owner does not want to use the key in admin url, then it can be set off from administrator settings.

My point in this post is, if you are a developer and creating a module that has Admin controller and you are simply calling some action of your controller lets say, mymodule/adminhtml_mycontroller/myaction/param1/1/param2/2 the it does not redirect you to your intended page, but will redirect to dashboard, IF key is enabled. You will have to add the key parameter to the URL to go to your page. So here is a simple snippet of code that will help you to build your URL with valid keys.

[source language=”php”] echo Mage::helper("adminhtml")->getUrl("mymodule/adminhtml_mycontroller/myaction/",array("param1"=>1,"param2"=>2)); [/source]

The “adminhtml” helper will automatically create url with keys attached to the URL.

Next thing, if you see the key of various pages in admin you will see that those keys are not same, there is a different logic behind creating those key values. The keys generated depends upon the controller and action you are about to execute. Keys can be seperatly generated as follows.

[source language=”php”] Mage::getSingleton(‘adminhtml/url’)->getSecretKey("adminhtml_mycontroller","myaction"); [/source]

Hope this helps, and this can be particularly helpful when you are using templates in admin modules and adding buttons to redirect to some other location. At least that was my case!