Magento 2 – Get Admin URL with Key and Parameters

adminbackendmagento2url

I don't understand why but in many topics I see that to get and admin route url with secure key and parameter I only need to use :

$this->urlBuilder->getUrl('adminhtml/myroutename/mycontroller/myaction', array('myparam' => 'myvalue'))

and urlBuilder I a Magento\Framework\UrlInterface object.

But in front, I get this URL: http://mysite.lan/adminhtml/myroutename/mycontroller/myparam/myvalue/

And if I remove the adminhtml I get this URL: http://mysite.lan/myroutename/mycontroller/myaction/myparam/myvalue/

So how can I create a correct admin URL with the key and the params?

Best Answer

If you are in an admin controller, you can just get the URL from the context (which is injected in the constructor):

$url = $this->context->getUrl();

$myUrl = $url->getUrl('adminhtml/myroutename/mycontroller/myaction', array('myparam' => 'myvalue'));

/This change with remove bracket/

Related Topic