Skip to content

Commit

Permalink
Move to _requestedSite() and combine with CRAFT_SITE check
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jan 2, 2025
1 parent e18382c commit 5d93583
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@
],
'sites' => [
'class' => craft\services\Sites::class,
'currentSite' => craft\helpers\App::env('CRAFT_SITE'),
],
'i18n' => [
'class' => craft\i18n\I18N::class,
Expand Down
28 changes: 15 additions & 13 deletions src/web/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use craft\helpers\StringHelper;
use craft\models\Site;
use craft\services\Sites;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\db\Exception as DbException;
use yii\di\Instance;
Expand Down Expand Up @@ -276,19 +277,6 @@ public function init(): void
}
}

// Finally if this isn't a control panel request and the site header has been passed, set the current site
$siteHandle = $this->getHeaders()->get('X-Craft-Site');
if (!$this->_isCpRequest && $siteHandle !== null) {
$site = $this->sites->getSiteByHandle($siteHandle, false);
// Fail silently if Craft isn’t installed yet or is in the middle of updating
if ($site === null && Craft::$app->getIsInstalled() && !Craft::$app->getUpdates()->getIsCraftUpdatePending()) {
/** @noinspection PhpUnhandledExceptionInspection */
throw new SiteNotFoundException('Site does not exist.');
}

$baseUrl = rtrim($site->getBaseUrl() ?? '', '/');
}

// Set the current site for the request
if ($this->sites instanceof Sites) {
$this->sites->setCurrentSite($site ?? null);
Expand Down Expand Up @@ -1529,6 +1517,20 @@ private function _requestedSite(?int &$siteScore = null): Site
return $site;
}

// Is CRAFT_SITE or X-Craft-Site present?
$siteId = App::env('CRAFT_SITE') ?? $this->getHeaders()->get('X-Craft-Site');
if ($siteId !== null) {
if (is_numeric($siteId)) {
$site = $this->sites->getSiteById($siteId, false);
} else {
$site = $this->sites->getSiteByHandle($siteId, false);
}
if (!$site && Craft::$app->getIsInstalled() && !Craft::$app->getUpdates()->getIsCraftUpdatePending()) {
throw new InvalidArgumentException("Invalid site: $siteId");
}
return $site;
}

$sites = $this->sites->getAllSites(false);

if (empty($sites)) {
Expand Down

0 comments on commit 5d93583

Please sign in to comment.