You can create a new setting in your settings.local.php
or settings.php
file like:
$settings['event_base_url'] = 'https://www.example.com';
or some people are adding switch block based on the server host, e.g.:
switch (@$_SERVER['HTTP_HOST']) {
default:
case 'dev.example.com':
$settings['event_base_url'] = 'http://dev.example.com';
break;
case 'stage.example.com':
$settings['event_base_url'] = 'https://test.example.com';
break;
case 'www.example.com':
case 'preprod.example.com':
$settings['event_base_url'] = 'https://live.example.com';
break;
}
Then reference it in the code like:
use Drupal\Core\Site\Settings;
$baseUrl = Settings::get('event_base_url', '');
So your code would look like:
use Drupal\Core\Site\Settings;
class EventSuggestionForm extends FormBase {
private $baseUrl;
private $apiUrl = '/api/v1/';
function __construct() {
// Read value from the settings file.
$this->$baseUrl = Settings::get('event_base_url', '');
}
...
Then change this setting per environment in the settings file, similar as you've different database credentials per different environment. This is the same concept as it's used for the existing $settings['file_public_base_url']
parameter.