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'
));
4

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).

  • Thanks, I will try it tomorrow. So this base area is the fallback when the file not found in the view/[area]/web, right? – Roland Soós Apr 14 '16 at 16:20
  • @RolandSoós Yes – Siarhey Uchukhlebau Apr 14 '16 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 – David Stone Mar 15 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.