I'm looking for clear solution without spikes, without creation any subfolders in pub with index files, without hardcoding cookies etc.
In other words I'd like to achive multistore in magento only using nginx configuration and magento stores/configuration.
I mostly achieved this, however I'm using two stubs, which I want to remove.
I've created some stores in admin area. Then I configured for all of them base_url in stores/configuration (also in admin area).
Let's have a look at nginx configuration. I've put into it one stub:
I added
map $http_host $MAGE_RUN_CODE {
sub1.domain.com "sub1_magento";
sub2.domain.com "sub2_magento";
sub3.domain.com "sub3_magento";
}
Then inside section for php files I've added this:
fastcgi_param MAGE_RUN_TYPE website;
fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;
In $_SERVER
variable MAGE_RUN_TYPE
has expected value - 'website', however variable $MAGE_RUN_CODE
doesn't have correct value.
For now I removed 'map' section and hardcoded value for it. If somebody knows how to solve this nginx issue - please tell me how.
Let's move to the main issue:
As I understand the main actions with store detection are happening inside
Magento\Store\Model\StoreResolver
If you put breakpoint inside constructor you may notice that $runMode
and $scopeCode
have expected values (from nginx config, as described above MAGE_RUN_CODE
for me is hardcoded).
Then let's have a look at function:
/**
* {@inheritdoc}
*/
public function getCurrentStoreId()
{
list($stores, $defaultStoreId) = $this->getStoresData();
$storeCode = $this->request->getParam(self::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie());
if (is_array($storeCode)) {
if (!isset($storeCode['_data']['code'])) {
throw new \InvalidArgumentException(__('Invalid store parameter.'));
}
$storeCode = $storeCode['_data']['code'];
}
if ($storeCode) {
try {
$store = $this->getRequestedStoreByCode($storeCode);
} catch (NoSuchEntityException $e) {
$store = $this->getDefaultStoreById($defaultStoreId);
}
if (!in_array($store->getId(), $stores)) {
$store = $this->getDefaultStoreById($defaultStoreId);
}
} else {
$store = $this->getDefaultStoreById($defaultStoreId);
}
return $store->getId();
}
Store code is taking from one of two places: request or cookie.
In my opinion in case with request is when we type url like this: http://sub1.domain.com/?__store=blablabla
. So it's not our case.
The second case (cookie) works only with store selector. (also not our case)
I hardcoded there necessary store name and I received expected result.
Did I miss anything? Is there any third case? How should magento detect which store/website to use?