Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi-storefront Magento #869

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

h3xx
Copy link
Contributor

@h3xx h3xx commented Mar 1, 2023

I really want to know if the method described under How multi-storefront switching works is the standard way of switching between storefronts, or if it's just a technique peculiar to my workplace.

This may be going too far towards how one hosting solution (Adobe's Magento Cloud service) sets things up; other hosting solutions are available.

My change

(Aside from a slight refactor to make it easier) this change adds an additional YAML file, compose.multi-storefront.yaml, that adds 2 additional volumes to app and phpfpm:

  • src -> /app - this mimics the directory layout when deployed to Magento Cloud.
  • src/php.ini -> /usr/local/etc/php/conf.d/local-php.ini - mapped so that PHP will load it.

It only uses compose.multi-storefront.yaml if both src/php.ini and src/magento-vars.php exist.

How multi-storefront switching works

Multi-storefront switching is performed by modifying the $_SERVER['MAGE_RUN_CODE'] and $_SERVER['MAGE_RUN_TYPE'] values inside of $_SERVER. It's done in a special file called magento-vars.php. Here's an example:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Enable, adjust and copy this code for each store you run
 *
 * Store #0, default one
 *
 * if (isHttpHost("example.com")) {
 *    $_SERVER["MAGE_RUN_CODE"] = "default";
 *    $_SERVER["MAGE_RUN_TYPE"] = "store";
 * }
 *
 * @param string $host
 * @return bool
 */
function isHttpHost(string $host)
{
    if (!isset($_SERVER['HTTP_HOST'])) {
        return false;
    }
    return $_SERVER['HTTP_HOST'] === $host;
}
/**
 * Enable, adjust and copy this code for each store you run
 *
 * Store #0, default one
 */
if (
    isHttpHost("www.defaultstore.example.com")
    || isHttpHost("defaultstore.example.com")
    || isHttpHost("defaultstore.example.dev")
) {
    $_SERVER["MAGE_RUN_CODE"] = "default";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}
if (
    isHttpHost("www.store1.example.com")
    || isHttpHost("store1.example.com")
    || isHttpHost("store1.example.dev")
) {
    $_SERVER["MAGE_RUN_CODE"] = "store1_store_view";
    $_SERVER["MAGE_RUN_TYPE"] = "store";
}

This file is included in every request via src/php.ini:

;
; Multi store support
;
auto_prepend_file = /app/magento-vars.php

Using our same example domains, getting this setup for local development depends on modifications to /etc/hosts to make both defaultstore.example.dev and store1.example.dev work in the browser:

127.0.0.1 ::1 defaultstore.example.dev
127.0.0.1 ::1 store1.example.dev

Running bin/setup-ssl defaultstore.example.dev store1.example.dev is not a bad idea either.

@what-the-diff
Copy link

what-the-diff bot commented Mar 1, 2023

  • The docker-compose script was changed to support multi-storefront.
  • A new compose file, compose/compose.multi-storefront.yaml, was added for the same purpose as above (1).

@piotrkwiecinski
Copy link
Contributor

This is not needed to run multistore. I managed to have running instance with adding a file for mapping a domain to run code:

I have a file store.map.conf with content:

map $http_host $MAGE_RUN_CODE {
    sub1.domain.loc sub1_store_code;
    sub2.domain.loc sub2_store_code;
    domain.loc default;
}

In docker-compose.dev.yaml:

services:
  app:
    volumes: &appvolumes
      - ./store.map.conf:/etc/nginx/conf.d/store.map.conf:cached

@markshust what do you think about adding readme for this instead of the PR.

The proposed solution isn't generic as magento-vars.php in cloud projects applied environment specific domains based on a branch you're on. It's used for mapping domains for environments on platform.sh not your local environment.

@markshust
Copy link
Owner

markshust commented Nov 21, 2023

@piotrkwiecinski I actually kinda like this PR. So often, I hear from devs that they do not know how to make updates to the Docker image/config and there are a few steps in play.

@h3xx so sorry for the delay, I've not been attentive to OSS lately. I like this PR.

To clarify, one would need to just create src/magento-vars.php and src/php.ini files to get the multi-store/domain support?

Also, does just adding a src/php.ini file work as expected, applying this update to the PHP config? This would allow someone to easily modify the PHP config without messing with the image config. I think just having a src/php.ini file to apply PHP config would make it so easy to modify this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants