Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 953
Sohan Avatar asked Dec 19 '22 15:12

Sohan


2 Answers

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);
    }
}
like image 103
Nahid Avatar answered May 12 '23 04:05

Nahid


You can easily get Admin url By calling

$this->getUrl('adminhtml/module/action');

Please not that "Context" type of object is loaded in the $this object

like image 23
Shine Avatar answered May 12 '23 05:05

Shine