Score:0

How to get the base URL of a site in Queue API?

co flag

I am trying to get the base url of the drupal 8 website in a queue process but I am getting http://default as the base url. What am I missing here?

  public function testUrl() {
    global $base_url;
    var_dump($base_url);
    $host = \Drupal::request()->getSchemeAndHttpHost();
    var_dump($host); // output - http://default
    $host2 = \Drupal::request()->getHost();
    var_dump($host2); // output - default
    $uri = \Drupal::request()->getUri();
    var_dump($uri); // output - http://default
    $url = \Drupal\Core\Url::fromUserInput('/', ['absolute' => TRUE])->toString();
    var_dump($url); // output - http://default
    $url_options = [
      'absolute' => TRUE,
      'language' => \Drupal::languageManager()->getCurrentLanguage(),
    ];
    $site_url = \Drupal\Core\Url::fromRoute('<front>', [], $url_options)->toString();
    var_dump($site_url); // output - http://default
    var_dump($_SERVER['SERVER_NAME']); // output - NULL
    die;
  }
Score:2
us flag

The process running the queue worker will be setting that default host. For example, drush cron

You may need to set the public host name with the drush request, via uri or use a drush alias.

drush --uri=http://example.com cron

More info https://docs.drush.org/en/8.x/usage/

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.