3

I have a file called image.png

Folder structure:

-app
 -code
 -Roland
 -HelloWorld
 -view
 -adminhtml
 -web
 -frontend
 -web
 -image.png

When I call this on frontend:

$this->getViewFileUrl("Roland_HelloWorld::image.png");

Result: http://localhost/magento2/pub/static/frontend/Magento/luma/en_US/Roland_HelloWorld/image.png

And the image loads fine.


When I call this on backend:

$this->getViewFileUrl("Roland_HelloWorld::image.png");

Result: http://localhost/magento2/pub/static/adminhtml/Magento/backend/en_US/Roland_HelloWorld/image.png

The image is not loading as it is not in the adminhtml/web/ folder.

Is there any way to load the static url for this asset with the frontend application area? What would be the official way, I do not want to duplicate this image into both folder?

UPDATE #1

This might be good, does anyone know better solution?

echo $this->getViewFileUrl("Roland_HelloWorld::image.png", array(
 'area' => 'frontend',
 'theme' => 'Magento/Luma'
));
asked Apr 14, 2016 at 14:34

1 Answer 1

5

You can try to use base folder inside View if you want to have the one file in frontend and backend both:

-app
 -code
 -Roland
 -HelloWorld
 -view
 -base
 -web
 -image.png

The next folder, view, is the area folder. Areas area a way to split individual Magento 2 applications into different areas of functionality, based on the URL. i.e. The cart application is Magento’s frontend area, the backend admin console is the adminhtml area.

So what’s the base area? This is one of those places where Magento 2 has improved on Magento 1 — the base area is a special folder that will allow you to serve your files from either the frontend or adminhtml areas. We’ll talk more about this below.

You can find more info here (source).

answered Apr 14, 2016 at 16:10
3
  • Thanks, I will try it tomorrow. So this base area is the fallback when the file not found in the view/[area]/web, right? Commented Apr 14, 2016 at 16:20
  • @RolandSoós Yes Commented Apr 14, 2016 at 16:21
  • Using base worked for me as well. However when moving an existing file from adminhtml to base I had to manually remove a broken symlink with something like: rm pub/static/adminhtml/Magento/backend/en_US/Roland_HelloWorld/image.png Commented Mar 15, 2019 at 20:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.