Valet-plus: How to handle multiple stores in Magento site?

Created on 3 Oct 2017  路  24Comments  路  Source: weprovide/valet-plus

What's the recommended way of handling multiple stores with Valet+?

Usually I'd just add a line in my nginx.conf file so the MAGE_RUN_CODE or MAGE_RUN_TYPE server variables change depending on the domain name, is it possible to do something like this with Valet+? Should I use a custom driver?

Thanks.

Enhancement

Most helpful comment

Though you shouldn't copy frontControllerPath instead do something like this:

<?php
class LocalValetDriver extends Magento2ValetDriver {
     public function frontControllerPath($sitePath, $siteName, $uri) {
        if ($_SERVER['HTTP_HOST'] === 'first.dev') {
            $_SERVER['MAGE_RUN_CODE'] = 'default';
            $_SERVER['MAGE_RUN_TYPE'] = 'store';
        }

        if ($_SERVER['HTTP_HOST'] === 'second.dev') {
            $_SERVER['MAGE_RUN_CODE'] = 'second';
            $_SERVER['MAGE_RUN_TYPE'] = 'store';
        }

        if ($_SERVER['HTTP_HOST'] === 'extra.dev') {
            $_SERVER['MAGE_RUN_CODE'] = 'extra';
            $_SERVER['MAGE_RUN_TYPE'] = 'website';
        }

        return parent::frontControllerPath($sitePath, $siteName, $uri)
     }
}

All 24 comments

Haven't tried it yet, but you might be able to run Magento from subdirectories and link your multi-store domain names to those directories.

Thanks, pointing the domain names is not a problem, e.g. I can point magento.dev and store2.magento.dev to the same directory just fine.

The problem is that I also need to let Magento know that if the domain name is store2.magento.dev set the MAGE_RUN_CODE server variable to store2 so it loads the right store view, right now going to the store2 domain just redirects me to the main magento.dev store as "expected".

@jahvi I usually handle this by modifying the index.php file to do the hostname checks:

$host = $_SERVER['HTTP_HOST'];
if (preg_match('/some-regex-that-matches-host-but-extracts-runcode-from-hostname/', $host, $matches)) {
    $_SERVER['MAGE_RUN_CODE'] = $matches[1];
}

I see I didn't want to edit the index file but it might be the easiest way, at least in development thanks!

I think this will be a less dirty way to handle multi stores:

  1. Add all stores domains using valet link i.e. valet link second to point a second.dev traffic to the current project
  2. Create LocalValetDriver.php file in the project root
  3. Extend create new LocalValetDriver class extending the Magento2ValetDriver class
  4. Copy the frontControllerPath function from https://github.com/weprovide/valet-plus/blob/master/cli/drivers/Magento2ValetDriver.php
  5. Add your stores configuration to the function body like this:
if ($_SERVER['HTTP_HOST'] === 'first.dev') {
    $_SERVER['MAGE_RUN_CODE'] = 'default';
    $_SERVER['MAGE_RUN_TYPE'] = 'store';
}

if ($_SERVER['HTTP_HOST'] === 'second.dev') {
    $_SERVER['MAGE_RUN_CODE'] = 'second';
    $_SERVER['MAGE_RUN_TYPE'] = 'store';
}

if ($_SERVER['HTTP_HOST'] === 'extra.dev') {
    $_SERVER['MAGE_RUN_CODE'] = 'extra';
    $_SERVER['MAGE_RUN_TYPE'] = 'website';
}
  1. Enjoy working multi store :)

@Igloczek's solution is 馃憣

Though you shouldn't copy frontControllerPath instead do something like this:

<?php
class LocalValetDriver extends Magento2ValetDriver {
     public function frontControllerPath($sitePath, $siteName, $uri) {
        if ($_SERVER['HTTP_HOST'] === 'first.dev') {
            $_SERVER['MAGE_RUN_CODE'] = 'default';
            $_SERVER['MAGE_RUN_TYPE'] = 'store';
        }

        if ($_SERVER['HTTP_HOST'] === 'second.dev') {
            $_SERVER['MAGE_RUN_CODE'] = 'second';
            $_SERVER['MAGE_RUN_TYPE'] = 'store';
        }

        if ($_SERVER['HTTP_HOST'] === 'extra.dev') {
            $_SERVER['MAGE_RUN_CODE'] = 'extra';
            $_SERVER['MAGE_RUN_TYPE'] = 'website';
        }

        return parent::frontControllerPath($sitePath, $siteName, $uri)
     }
}

Thanks, I was looking for something like this! Works like a charm.

Do you think it's possible to store this type of configuration somewhere and add the logic directly to the main driver?
I have a lot multisite stores for both M1/M2, will be great to find out the best possible way and write it down somewhere in the docs.

@Igloczek in theory you could read from a json file, I'd be willing to add something like that to the driver, since we have a need for that too at We Provide 馃憤

cc @samgranger

Are you thinking about adding a new one, like multistore.json at the project root or there is a config file somewhere already and we can extend it?

@Igloczek yeah something like that. Or stores.json

Why not go a little generic and something like server_vars.json or host_vars.json so that you could pass in things other than just MAGE_RUN_CODE? (could be useful for other drivers)

Closing this ticket as we have added this functionality to 1.0.19. Documentation coming soon!

@samgranger Could you link the documentation for this functionality? Thanks!

Quick example @danistor - create a new file called .env.valet in your project root.

Example contents:

<?php

return [
    'otherstoreview' => [
        'MAGE_RUN_CODE' => 'store_code_goes_here',
        'MAGE_RUN_TYPE' => 'store',
    ],
];

valet link otherstoreview creates a new link, you can now view a store with code store_code_goes_here at otherstoreview.test

valet link otherstoreview has to be run in the root of your current magento project by the way - in case that part was unclear.

It doesn't seem to work, I am redirected to the default store. Also tried with

<?php 
return [
    'otherstoreview' => [
        'MAGE_RUN_CODE' => 'website_code',
        'MAGE_RUN_TYPE' => 'website',
    ],
];

valet links correctly shows that the link was created. Am I missing something?

Did you change the base URL for the second store in the backend?

My bad! after changing the url for the store everything works like a charm. Thanks!

@samgranger Could you link the documentation for this functionality? Thanks!

I'm struggling to find this documentation, could you point me in the right direction please? Thanks.

Quick example @danistor - create a new file called .env.valet in your project root.

Example contents:

<?php

return [
    'otherstoreview' => [
        'MAGE_RUN_CODE' => 'store_code_goes_here',
        'MAGE_RUN_TYPE' => 'store',
    ],
];

valet link otherstoreview creates a new link, you can now view a store with code store_code_goes_here at otherstoreview.test

I just used this configuration to handle a multi-store magento installation and wokerd as expected. Solution verified.

When I attempt to configure this as described, the admin URL returns a 404, any ideas?

@matt-bailey I've added a Wiki page with a summary of the Environment variables information here https://github.com/weprovide/valet-plus/wiki/Environment-variables
You can open a new issue if anything is missing or needs changed.

Was this page helpful?
0 / 5 - 0 ratings