Roots Discourse

How to get the path to the /dist folder via php, but not in a blade template

In the Sage book, it says “you would use the @asset directive”. Looks nice and tidy.

Is there a ‘proper’ way to do this in a normal php file?

<?php echo get_template_directory_uri(); ?>/../dist/

^^ this works but not sure if that’s by fluke or by design…

Look into what the @asset directive does, it could lead you to an answer.

asset_path() comes from is defined in /app/helpers.php

Which is using roots/sage-lib.

Also helpful: https://github.com/roots/sage/blob/master/config/assets.php

2 Likes

Ahaaa stuff like:

<?php echo App\asset_path('styles/login.css'); ?>

outputs: /app/themes/sage/dist/styles/login.css

thanks :+1:

3 Likes

This Works for me :

<?php echo get_template_directory_uri() . '/../dist/images/home-banner/home-banner.mp4' ?>

It may give you the result you expect, but you should not trust it.

Files in the dist directory have been put through the build process, and for some of them (notably (s)scss and js files) the output receives a hash when built for production–i.e. main-38fh299.css. When not built for production, it has no hash–i.e. main.css. The hash is for cache-busting purposes, which is why it isn’t generated in non-production builds. The build process outputs a manifest, which maps the hashed names to their original names, allowing them to be easily referenced. The helper functions in Sage–i.e. asset_path(), @asset(), etc–take this into account, and will return the path to the hashed asset. The approach I’ve quoted above will not. It will appear to work locally, but when you make a production build those links will break.

In any event, this is a 3-year-old thread that doesn’t need updating, and I’m only replying because the above is bad practice and I don’t want someone coming here and thinking they should do it that way. I’ll be locking this thread, since this issue is resolved and will not benefit from additional discussion. If you have a problem related to asset paths/uris, please create a new thread.