How to fetch getUrl() in Plugin or Observer file in magento 2?

By default you cant call $this->getUrl() method in Plugin or Observer file. When you need to call getUrl() function of Magento in plugin or observer file you need to pass dependancy as Magento\Framework\UrlInterface to construct() method.

Now In plugin or Observer function we can get getUrl() as below way,
$this->urlBuilder->getUrl(‘sales/order/view’, [‘order_id’ => 1]);

Run below command for get effect of our Dependancy Injection to code,

Above url will return as,
http://{yoururl.com}/admin/sales/order/view/order_id/1/

Leave a Reply

Your email address will not be published. Required fields are marked *