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

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

        $category = $this->categoryFactory->create();

        /** @var \Magento\Store\Model\Website $website */
        $website = $this->websiteFactory->create();

            /** @var \Magento\Store\Model\Group $group */
            $group = $this->groupFactory->create();
            $group->setName('main '.$nom);

        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();
                $group = $this->groupFactory->create();
                $group->load('main '.$nom, 'name');
                $store->setName($lang.' '.$nom);
                // 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 ?


1 Answer 1

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

        $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);
            'web/unsecure/base_link_url', '{{unsecure_base_url}}', 'websites', $websiteId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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