Contents
Where do I find media url in Magento?
Usage of Object Manager is discouraged as per Magento’s coding standards. In a template file, we can get the media URL using the following code: As per the Magento’s recommendation, the document root of your website should be outside pub directory during development and it should be inside pub directory when moved to production.
How to get image from theme in Magento 2?
We will get image from theme or from module/extension in Magento 2. we hope this blog is useful for you. let me know in comment if you have any questions. thanks!
How to get media url from Template file?
In a template file, we can get the media URL using the following code: As per the Magento’s recommendation, the document root of your website should be outside pub directory during development and it should be inside pub directory when moved to production. Hence we cannot hard code the pub directory while getting media URL in the template files.
Is there any built-in Magento function which handles this?
Is there any built-in Magento function which handles this, so I do not have to create my own helper? As @Khoa TruongDinh states you can use the Magento\\Framework\\View\\Asset\\Repository to get a file class Magento\\Framework\\View\\Asset\\File, which you can then use to get various data.
How to get pub / static file path in Magento?
To get asset in your theme folder app/design/frontend/VENDOR/THEME/web simply strip the module name as below…. See Magento\\Framework\\View\\Asset\\File for functions you can use to get file data. For example: your image is under : app/code/Vendor/Module/view/frontend/web/images/image.png
Where do I Find my assets in Magento?
As @Khoa TruongDinh states you can use the Magento\\Framework\\View\\Asset\\Repository to get a file class Magento\\Framework\\View\\Asset\\File, which you can then use to get various data. To get asset in your theme folder app/design/frontend/VENDOR/THEME/web simply strip the module name as below….