Cms: Parsing of site.baseUrl not happening properly w/ multisites config

Created on 25 Jan 2019  路  5Comments  路  Source: craftcms/cms

Description

Working on a language switcher on a multi-sites multilingual site.

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

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

Steps to reproduce

  1. try to enter $BASE_URL/en in the CP > Craft adds "http://" in front upon save
  2. Set site URL to just $BASE_URL in the CP
  3. Loop through all sites to get their baseUrl
{% set allSites = currentSite.group.sites %}

{% for site in allSites %}
  {{ site.baseUrl }}
{% endfor %}
  1. Whenever you try to output 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.

Additional info

  • Craft version: 3.1.4
  • PHP version: 7.1.8
  • Database driver & version: MySQL 5.6.35
  • Plugins & versions:

Most helpful comment

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.

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings