Jump to content

Multi-Site, adding entries into index.config.php


swampmusic
 Share

Recommended Posts

Hi Folks,

We have a multi-site #1, multiple sites, multiple databases platform in development/concept at the moment.

A company can sign up, register, and PW then creates a db, copies over a site folder with /wire symlink. All seems to work well, after sign up they can then enter into their subdomain site. ( Because of this dirty hack below that inserts the new subdomain into PW's hostSiteConfig array. Without this, the subdomain is not correctly registered as a PW site & instance I think).

Does anyone know how to dynamically add an entry into the index.config.php file ? Knowing the little I have learnt so far about PW, Ryan has probably left us a really smart way to do it, but I haven't figured it out or found it yet. 

I have done a dirty hack on it, which I'm not very happy or proud off. Anybody know of a better way around the problem ?  ... below is more or else the code I am using in the pilot version.

 
// some error traps taken out for brevity on the booleans below...
 
$hostsArray = ProcessWireHostSiteConfig();
// $sitename normally populated from the customers signup/registration
// hard coded here for this example...
$sitename ='somesubdomain';
 
$a = $sitename . '.' . 'localhost:8020';     // machine name, port & protocol normally comes from $config, hard coded for example.
$b = 'site-' . $sitename . '/site';
$newSite = array($a => $b);
 
$hostsArray = array_merge($hostsArray, $newSite);
 
// very dirty hack starting....
// creating a string copy of the array used in
// index.config.php
$newString='';
foreach($hostsArray as $key => $value) {
$newString .= "'" . $key . "' => '" . $value . "', ";
}
$newString = "return array(" . $newString . ');}';
 
// dirty hack getting worse :-(
$newConfigIndex = file_get_contents($config->paths->root . 'index.config.php');
$startpos = strpos($newConfigIndex, 'return array(');
 
// quality coders starting to feel sick at this point i suspect... sorry! ;-(
$newConfigIndex = substr_replace($newConfigIndex, $newString, $startpos);
 
$boolConfigIndex = file_put_contents($config->paths->root . 'index.config.php', $newConfigIndex);
// crikey! did he really just do that !!!!!!
 
 
Link to comment
Share on other sites

I haven't looked at your code closely, but if you need to write to a file, you need to write to a file. You have file_put_contents and fwrite. I see no other way around it irrespective of the software you use. ProcessWire itself writes to /site/config.php/ during install. I use this multi-site setup myself. I've been meaning to write some code or a module to automate the process but never got round to it. You could do with some error checking and backing up the original file as part of the updating the file.

  • Like 1
Link to comment
Share on other sites

@abdus, it has to be done from php file dynamically during the registration process. I think PW is monitoring the index.config.php file. Once new domain has been added into it, it then seems to work ok for a customer to log onto their new site and not get the error in admin that you get when a multi-site site is not in the index.config.php

@kongondo, thx re: ideas on code.

Normally in PW we have  been left with hooks and API's to deal with things when we need to dig a bit deeper. I just can't find anything for the index.config.php file. Seems a bit odd if I need to hack it like this, otherwise creating multi-sites must always be a manual process.

Link to comment
Share on other sites

@kongondo You wont be creating these files manually of course, you extend logic for the signup form to create the files for you. Then in index.config.php, you parse, merge and return the array. Something like this

function ProcessWireHostSiteConfig() {
    $configDir = __DIR__ . '/.config/';

    $files = wire()->files;
    $siteConfigs = $files->find($configDir, [
        'extensions' => ['yaml']
    ]);

    $sites = [];
    foreach ($siteConfigs as $cfg) {
        $config = yaml_parse_file($cfg);

        // needs better sanitization
        $isValidConfig = true;
        if (!$isValidConfig) continue;

        $sites[$config->domain] = $config->siteDir;
    }

    $sites['*'] = 'site';
    return $sites;
}

 

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...