1

We have a multisite setup. We're wanting to use the same codebase, but deploy to two different servers with some of the sites on server 1 and the rest on server 2.

For example:

  • Server 1
    • Site 1 [Primary site]
    • Site 2
  • Server 2
    • Site 3 [Primary site]
    • Site 4

Currently sites 1-4 are all enabled and Site 1 is set as the Primary site.

On deployment we want to enable/disable the relevant sites, so that within the Admin Control Panel users will only see the sites available on that server. Similarly, we want to set the primary site for each server.

We're trying to do this programmatically so that it can be run as part of our deployment scripts. However, when we call saveSite although it is returning true the database and config files are not actually updating.

For example, to update the primary site we're trying the following:

$site = Craft::$app->sites->getSiteByHandle('site3');
$site->primary = true;
Craft::$app->sites->saveSite($site); // returns true

Looking at the Craft code, it looks like this should be moving the primary site flag from the current primary site to site3, but this isn't happening.

What do we need to do in order to make this update correctly?

2
  • Running into the same issue in my codebase. Haven't had any success either. Running $app->sites->saveSite doesn't seem to actually save anything.
    – tekstrand
    Commented Dec 17, 2021 at 23:03
  • @drmonkeyninja did you eventually solve it? I have a similar task and looking for an solution. if it worked with the code above, how did you put into to your deployment scripts? via a module? Thanks!
    – Henrik
    Commented Dec 12, 2023 at 13:27

0

Your Answer

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

Browse other questions tagged or ask your own question.