Magento2 – How to Programmatically Change Base URL of Store

base-urlmagento2

i setup websites / store / storeview in code from controller with this :

public function createWebsite($nom, $language){
        $code = $this->transformerEnURL($nom);

        $category = $this->categoryFactory->create();
        $category->load($nom);
        $category->setName($nom);
        $category->setParentId(1);
        $category->setIsActive(true);
        $this->categoryRepositoryInterface->save($category);

        /** @var \Magento\Store\Model\Website $website */
        $website = $this->websiteFactory->create();
        $website->load($nom);
        if(!$website->getId()){
            $website->setCode($code);
            $website->setName($nom);
            $website->setDefaultGroupId(3);
            $this->websiteResourceModel->save($website);
        }

        if($website->getId()){
            /** @var \Magento\Store\Model\Group $group */
            $group = $this->groupFactory->create();
            $group->setWebsiteId($website->getWebsiteId());
            $group->setName('main '.$nom);
            $group->setRootCategoryId($category->getId());
            $group->setDefaultStoreId(3);
            $this->groupResourceModel->save($group);
        }

        foreach ($language as $value) {
            $lang = 'Français';
            if ($value == 'fr')$lang = 'Français';
            if ($value == 'en')$lang = 'English';
            if ($value == 'es')$lang = 'Espanol';
            if ($value == 'it')$lang = 'Italiano';
            if ($value == 'de')$lang = 'Deutsch';
            if ($value == 'ro')$lang = 'Român';
            /** @var  \Magento\Store\Model\Store $store */
            $store = $this->storeFactory->create();
            $store->load($value.'_'.$nom);
            if(!$store->getId()){
                $group = $this->groupFactory->create();
                $group->load('main '.$nom, 'name');
                $store->setCode($value.'_'.$code);
                $store->setName($lang.' '.$nom);
                $store->setWebsite($website);
                $store->setGroupId($group->getId());
                $store->setData('is_active','1');
                $this->storeResourceModel->save($store);
                // Trigger event to insert some data to the sales_sequence_meta table (fix bug place order in checkout)
                $this->eventManager->dispatch('store_add', ['store' => $store]);
            }
        }
    }

The foreach is for language obviously.

Now i need to change base Url of store view, I tried :

  • $store->setBaseUrl('http://www.url.com');
    But i got error on it and don't know what is the correct method.

  • $store->setConfigData('web/unsecure/base_url','http://www.url.com','stores',$store->getiD());
    But i don't know how to use it, i tried after the save of $store, i tried to reload a store but still get error.

So first, am I in the good way ? How can I setup base url ?

Regards,

Best Answer

protected $config;

protected $storeManager;

public function __construct(
    ...
    \Magento\Config\Model\ResourceModel\Config $config,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    ....
) {
    ....
    $this->config = $config;    
    $this->storeManager = $storeManager;
    ....
}

Now you can use this like, but after creating website,group, and store

        $storeBaseUrl=$this->storeManager->getStore()->getBaseUrl();    
        $storeMainUrl = $storeBaseUrl . "CANBEANYTHING" . '/'; // replace "CANBEANYTHING" with your unique url
        $websiteId=1;// replace with your dynamic id            
        $this->config->saveConfig('web/unsecure/base_url', $storeMainUrl, 'websites', $websiteId);
        $this->config->saveConfig(
            'web/unsecure/base_link_url', '{{unsecure_base_url}}', 'websites', $websiteId
        );        
Related Topic