4

I have a volume called "Website" with a File System Path that uses an environment variable, $ASSET_ADHOC_PATH. The variable has been setup in my .env file as follows:

ASSET_ADHOC_PATH="/home/theadhoc/craftcms/web/assets/theadhocracy"

The Volume has the following in the File System Path input:

$ASSET_ADHOC_PATH/website

That should resolve to my actual folder location, but when I upload a file it just creates a folder at:

/home/theadhoc/craftcms/web/$ASSET_ADHOC_PATH/website

If I get rid of the environment variable and use the literal value then it works fine (which is what I'm doing for now) but I'd like to get to the bottom of why this isn't working as I would expect.

EDIT: Just in case this helps, I've discovered I have the same issue when using (different) environment variables in the Base URL field too. Am I missing a step in setting up env vars? Do I need to add them to general.php or something as well? They seem to resolve without issue in the dropdown list, which accurately displays the variable contents.

1 Answer 1

5

If you use environment variables, you have to be aware that you can't add anything to it in your fields. What this means is that $ASSET_ADHOC_PATH/website in your CP will not be parsed. If you use environment variables, the only thing you should have in the field is the variable, nothing else.

To do what you are trying to do, you might want to consider Aliases. These are great for the Control Panel because you can add to them so @aliasName/website will be parsed and work the way you expect.

So in your case (and to get you going) with this ASSET_ADHOC_PATH variable set in your .env file, add the below in your config/general.php:

'aliases' => [
    '@assetAdHocPath' => getenv('ASSET_ADHOC_PATH')
],

Then in your volume settings use @assetAdHocPath/website and this should now work for you.

For the second part of the question, I'm guessing you may be trying to access an ENV in your template in which case you would use {{ getenv('ASSET_ADHOC_PATH') }} OR if you go the aliases way, {{ alias('@assetAdHocPath') }}.

3
  • 1
    Thanks, worked like a charm and learnt something new. Also, FYI for anyone else, aliases must go within one of the existing options in general.php to show up (i.e. in *, dev, production or staging). Jun 23, 2019 at 17:30
  • Yes sorry, I should have been more specific with that though if you’re going to use ENVs, it safe to put this in ‘*’. Glad you’re sorted!
    – Oli
    Jun 23, 2019 at 18:15
  • @theAdhocracy this fixed the issue I was having with my aliases not showing up. Needed to move the aliases block down into the '*' config. Thanks!
    – Panda4Man
    Oct 6, 2021 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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