Get Url in phtml files

Get Url in cms pages or static blocks


Below is a block class of my custom module. I have injected object of StoreManagerInterface & UrlInterface in the constructor of my module’s block class. Both of them can be used to fetch base and current URL. In below class, in function getStoreManagerData(), object of StoreManagerInterface is used to print the base and current url and in function getUrlInterfaceData() function, object of UrlInterface is used to print the base and current url.

Type variable :

With __construct :

Then you can Prining URLs using StoreManagerInterface

And you can Prining URLs using URLInterface

As you can see in the above code, my block class is extending class MagentoFrameworkViewElementTemplate. Hence, I can easily get URL and base URL in my template (.phtml) file with the following code:

Or Using Object Manager

Thank for reading this post, Hope it helps.

Leave A Comment?

You must be logged in to post a comment.