Magento is a great ecommerce platform.It is a little complicated at times but it does provides many methods and functionalities to perform some tasks easily.Today we will talk about a method of Magento 2 get view or skin url in your phtml file.I try to explain things simply in this tutorial to make learning easy and simple.
I suppose you are already familiar with magento 2 and its working methodology hence you have reached this point.Below are some methods and examples.Lets start with an example where you want to learn how to get static url and skin url in magento 2.
Suppose your image is located in folder web/images.
Getting Url with image path and name
simply add the following code with image path and image name in your code.
<?php echo $block->getViewFileUrl('images/test.png') ?>
Add in CMS
And if you want to add in cms page simply add with image path.
Get specific module in a folder
Similarly if you want to get specific module image in web folder,it can be done by following code. In my case Module name is QaisarSatti_HelloWorld.You will have to specify your own module name.
<?php echo $block->getViewFileUrl('QaisarSatti_HelloWorld::images/test.png'); ?>
Image url in block file
Also If you want to get image url in your block file.Following piece of code will get the job done.
I hope the above examples and codes are simple and effective. Furthermore if you want to see where $block->getViewFileUrl() method is defined. You can see every method Magento\Framework\View\Element\Template file extends from Magento\Framework\View\Element\Template that extends with Magento\Framework\View\Element\AbstractBlock.php.
You will be able to see that on line 762 this method getViewFileUrl is defined.
That’s it from today’s tutorial. I hope this will help you learn.
Last Modified: September 30, 2018