Last active
November 19, 2020 01:58
-
-
Save LostKobrakai/9c02484a9710e16bc512 to your computer and use it in GitHub Desktop.
Multisite routing for ProcessWire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// source: https://processwire.com/talk/topic/680-multiple-sites-from-one-install/?p=8778 | |
class Multisite extends WireData implements Module, ConfigurableModule { | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'Multisite', | |
'version' => 1, | |
'summary' => 'Allows multiple sites with different domains run from single PW-site and database.', | |
'singular' => true, | |
'autoload' => true, | |
); | |
} | |
public function init() { | |
$subdomains = explode("\n", strtolower($this->subdomains)); | |
foreach($subdomains as $subdomain) { | |
$httpHost = strtolower(wire('config')->httpHost); | |
if(strpos($httpHost, $subdomain) !== false) { | |
$this->subdomain = $subdomain; | |
// change path like /about/contact/ to /campaign.com/about/contact/ | |
$_GET['it'] = "/{$subdomain}/" . ltrim($_GET['it'], '/'); | |
// hook for path method to make path() and url() methods context aware | |
$this->addHookAfter('Page::path', $this, 'modifyPath'); | |
} | |
} | |
} | |
public function modifyPath($event) { | |
$event->return = str_replace("{$this->subdomain}/", '', $event->return);; | |
} | |
static public function getModuleConfigInputfields(array $data) { | |
$fields = new InputfieldWrapper(); | |
$modules = wire('modules'); | |
$field = $modules->get("InputfieldTextarea"); | |
$field->name = "subdomains"; | |
$field->label = "Other sites running from same install"; | |
$field->description = "Add each domain for their own line. Don't add http or https, just simple domain like: www.example.com tai campaign.example.com"; | |
$field->notes = "IMPORTANT: create homepage for each of these domains right on the root. Name should be exactly same as the domain you use here."; | |
$field->attr('value', $data['subdomains']); | |
$fields->add($field); | |
return $fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment