Working on a language switcher on a multi-sites multilingual site.
When trying to save something like $BASE_URL/en in the CP, Craft adds "http://" in front of it when I save it in the CP. In my .env, that var is set to http://mylocalsite.craft.test (no trailing slash)
Additionally, I get strange results in templates even when simply using $BASE_URL with nothing added to it in a multi site configuration when I try to use it in a template.
$BASE_URL/en in the CP > Craft adds "http://" in front upon save$BASE_URL in the CPbaseUrl {% set allSites = currentSite.group.sites %}
{% for site in allSites %}
{{ site.baseUrl }}
{% endfor %}
site.baseUrl, the environmental variable is not parsed so you get something like "http://yourcurrenturl/$BASE_URL"Solved it by using Yii alias like @baseUrl in the CP (mapped to .env variable) and using alias(site.baseUrl) in my templates.
I can reproduce the issue
On a side note, it might be good to standardize on one way of dealing with paths and URL in various environments.
Personally, I think Yii aliases make a lot of sense in the CP and templates, while .env variables are for (local) config files only. At least that's the way I see it.
Try to use site.getBaseUrl() instead.
I also noticed that variable interpolation $BASE_URL/en is currently not supported and you have to define a dedicated environment variable for every site $BASE_URL_EN. Even though it鈥檚 probably good practice to set them up that way, it would avoid confusion if it worked the same as aliases do, where @baseUrl/en works just fine.
It鈥檚 a little awkward but this is expected behavior. (Having both values accessible is crucial to being able to remember the _actual_ value, so it doesn鈥檛 get lost when you re-save your site settings down the road.) As @carlcs pointed out, if you use site.getBaseUrl() instead, it will return the parsed URL.
Thanks for the detailed info guys. Very useful as always.
As I said, I'll be sticking to using .env variables only in config files and Yii alisases for CP and in templates to keep what's left of my sanity ;o). Just tested getBaseUrl() with alias and it indeed gets parsed.
Now I really have to remember to go dig into the craft 3 class reference when I am puzzled like that.
Most helpful comment
Try to use
site.getBaseUrl()instead.I also noticed that variable interpolation
$BASE_URL/enis currently not supported and you have to define a dedicated environment variable for every site$BASE_URL_EN. Even though it鈥檚 probably good practice to set them up that way, it would avoid confusion if it worked the same as aliases do, where@baseUrl/enworks just fine.