Working with subdomains in MODx

Here from a friend there was a question:
good Day!

Read the article about friendship ModX and LS, and decided to ask the question he does in ModX can't decide how to implement the logic Revo depending on the current subdomain of the main site?

I see pro-cent.ru that subdomain imposed in the name of the city (http://kaliningrad.pro-cent.ru/). ModX somehow helps in the treatment of this, or are You hands and your code made?


Decided to give a response in the audience, surely someone is also useful.

In General on Habre already wrote that to work with multiple domains (or more precisely switching between them), you need to use the $modx- > switchContext($ctx); This is certainly a very useful thing, but in our case it won't save us, as in the case of switching to another context, we have lost the ability to just turn to the documents of our underlying context (in this case web) and the settings of contexts there is a subtlety. In General, $modx- > switchContext() is needed when the target context is completely independent, has its own documents, etc.

In my case I have a basic context and it already has all the documents necessary to me, and the contexts in the cities a La the krasnodar only need to reset some settings (for each context have its own settings the type of the underlying domain, city, code Ya., Metric, etc.), in General all to easy to determine what share of the output, etc.

So, I advise everyone to pay more attention to plugins. It is not so much to dedicate to those, but that's plugins allow anything to drive a wedge into the logic of MODx, with absolutely no touching the code of the engine.

In General, in order to solve our problem, do the following:

1. We need to create contexts and set them to your desired settings.

2. Create the plugin and specify the execution event OnHandleRequest

3. In the plugin, write the following (or similar)

the
<?php

// Here you can split the logic depending on the context
switch($modx->context->key){
case 'web':
break;
default: return;
} 


// Try to determine the domain of 3rd level and determine whether this is the context
if(preg_match('/([^\.]+)\.[^\.]+\.[^\.]+$/',$_SERVER['HTTP_HOST'], $match)){
if($ctx = $modx- > getObject('modContext', $match[1])){
// Perform the initialization of the context
if($ctx->prepare()) {
// Reload settings for MODx and
// Just keep in mind that these changed settings can be obtained only through the API $modx- > getOption();
// The templating engine not to overload, that is, if the chunk you have written [[++site_name]], the hack does not affect
// be careful
$modx- > config = array_merge($modx->config, $ctx->config);
}
}
}

// Settings defined by the main host
// Someone he is not obyazatelno, but I have in the event of a mismatch the host there is a redirect, 
// for some stupid crawler 
// spawn podosenov
if(!$host && (!$host = $modx- > config['mainHost'])) {
// print "unknown host for context." $modx->context->key.
return;
} 


// print $host;
$redir_host = ";
$redir_url = ";

// Here you can stretch logic when executing various events
switch($modx->event->name){
case 'OnPageNotFound': 

break;
case 'OnHandleRequest':

break;

default: ;
}

// Check for compliance with the primary host
if(!preg_match('/^'.$host.'$/i', $_SERVER['HTTP_HOST'])){
$redir_host = $host;
$redir_url = $_SERVER['REQUEST_URI'];
}

// If the host settings do not match, then do the 301-th redirect (redirect every write)
if($redir_host || $redir_url){ 
$params = array();
$redir_url ? $params['url'] = $redir_url : "";
$redir_host ? $params['hostl'] = $redir_host : "";
$modx- > runSnippet('redirect', $params);
}


Actually all.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Monitoring PostgreSQL + php-fpm + nginx + disk using Zabbix

Templates ESKD and GOST 7.32 for Lyx 1.6.x

Customize your Google