Add store codes to urls is set to yes.

Store code is: test.

  • Default homepage for that store loads correctly: home cms page for that particular store.

  • In the menu the store code is added to the urls: www.example.com/test/men.html

But when I add this code in a template file (one called in the homepage for example):

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$store = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
echo "Code: ".$store->getStore()->getCode(); 
echo "Name: ".$store->getStore()->getName(); 

I get the default store code and default store code name.

Why is that? I didn't edit the index file or htaccess.

If I would go ahead and add this to the index file:

 $params = $_SERVER;
 $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'test'; 
 $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
 $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
 $app = $bootstrap->createApplication('Magento\Framework\App\Http');

Then it will show the right store code, but I don't want to edit the index file as the client must be able create the stores in the admin without us editing files.


The issue was that the objectmanager was creating a new instance of the StoreManagerInterface. If I change it to


then I get the right store code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.