Last active
December 18, 2019 17:51
-
-
Save eonarik/be128b370b7efef1cba80b1b30a99dba to your computer and use it in GitHub Desktop.
modx plugin multiSite
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 | |
/** | |
* v1.0.7 | |
* переключение контекстов в зависимости от домена и/или урла | |
* | |
* необходимые для работы настройки контекста | |
* error_page | |
* site_start | |
* base_url - если нужно переключать контекст в пределах сайта (указывается раздел, напр. "/folder/") | |
* http_host - если контекст на другом домене (поддомене), напр. "http://site.ru" | |
* site_url - "{server_protocol}://{http_host}{base_url}" | |
* */ | |
if ($modx->context->key != 'mgr') | |
{ | |
$var = $modx->context->getOption('request_param_alias', null, 'q'); | |
$domains_array = []; | |
foreach ($modx->getIterator('modContext', [ | |
'key:not in' => ['web', 'mgr'] | |
]) as $ctx) | |
{ | |
$settings = [ | |
'http_host' => $ctx->getOption('http_host'), | |
'ctx' => $ctx->key, | |
]; | |
foreach ($ctx->ContextSettings as $ctxSettings) | |
{ | |
$settings[$ctxSettings->key] = $ctxSettings->value; | |
} | |
$domains_array[] = $settings; | |
} | |
if (!empty($domains_array)) | |
{ | |
switch ($modx->event->name) | |
{ | |
case 'OnHandleRequest': | |
foreach ($domains_array as $arr) | |
{ | |
if ( | |
$arr['ctx'] | |
&& ( | |
!empty($arr['base_url']) | |
&& strpos($_REQUEST[$var], ltrim($arr['base_url'], '/')) === 0 | |
) | |
|| ( | |
!empty($arr['http_host']) | |
&& $arr['http_host'] == $_SERVER['HTTP_HOST'] | |
) | |
) | |
{ | |
if (!empty($arr['base_url'])) | |
{ | |
$_REQUEST[$var] = str_replace(ltrim($arr['base_url'], '/'), '', $_REQUEST[$var]); | |
} | |
$modx->switchContext($arr['ctx']); | |
break; | |
} | |
} | |
break; | |
case 'OnPageNotFound': | |
// // если страница не найдена в определенном контексте - ищем ее в web | |
// if ( | |
// $modx->context->key != 'web' | |
// AND $uri = $_REQUEST[$var] | |
// AND $resource_id = $modx->findResource($uri, 'web') | |
// ) | |
// { | |
// $modx->switchContext('web'); | |
// $modx->sendForward($resource_id); | |
// } | |
// break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment