How to get the url for admin page (including the key) in magento 2x custom module controller

Sohan picture Sohan · Jun 29, 2016 · Viewed 10.8k times · Source

I need the page url in Magento 2x including key in my custom module controller. here something similar but this is for magento 1x. i need for magento 2x.

for magento 1x : Mage::helper('adminhtml')->getUrl('adminhtml/framexport/index') but i need similar for magento 2x.

Answer

Nahid picture Nahid · Sep 28, 2016

The right way is, inject the UrlInterface in you model block or whatever class constructor

Then call the getUrl() function

class SomeClass extends \Some\Other\Class
{

    protected $_backendUrl;

    public function __construct(
        ...........
        ...........
        \Magento\Backend\Model\UrlInterface $backendUrl,
        ...........
    ) {

        $this->_backendUrl = $backendUrl;
    }
    public function someFunction()
    {
        $params = array('some'=>'url_parameters');

        $url = $this->_backendUrl->getUrl("the/url/path", $params);
    }
}