📜 ⬆️ ⬇️

Work with subdomains in MODx

Here a question came from a friend:
Good afternoon!

I read an article about the friendship of ModX and LS , and decided to ask a question that I myself can’t decide in ModX how to implement the logic of Revo depending on the current subdomain of the main site?

I see on pro-cent.ru that the name of the city is placed in the subdomain (http://kaliningrad.pro-cent.ru/). Does ModX help in handling this, or is everything done with your hands and your code?

')
I decided to give an answer to the audience, probably someone will come in handy too.

In general, it was already written on Habré that to work with several domains (more precisely, switching between them), you need to use the $ modx-> switchContext ($ ctx) method; This is certainly a very useful thing, but in our case it does not save us, as in the case of switching to another context, we lose the opportunity to just access the documents of our basic context (in this case, the web), as well as with the subtlety context settings there is. 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 I need, and contexts in cities a la krasnodar are needed only to reload some settings (each context has its own settings such as basic domain, city, code I .Metrics, etc.), in general, all that is easy to determine which shares to display, etc.

So, I advise everyone to pay more attention to plugins. They are not so much dedicated to those, but it is the plugins that allow you to arbitrarily wedge into the logic of MODx, while not touching the code of the engine itself.

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

1. Create the necessary contexts and write the necessary settings in them.

2. Create a plugin and specify the execution of the OnHandleRequest event

3. In the plugin we write the following (or something like that)

<?php //         switch($modx->context->key){ case 'web': break; default: return; } //    3-   ,     if(preg_match('/([^\.]+)\.[^\.]+\.[^\.]+$/',$_SERVER['HTTP_HOST'], $match)){ if($ctx = $modx->getObject('modContext', $match[1])){ //    if($ctx->prepare()) { //   MODx- //   ,         API $modx->getOption(); //   ,         [[++site_name]],       //   $modx->config = array_merge($modx->config, $ctx->config); } } } //      // -   ,         , //  -    //    if(!$host && (!$host = $modx->config['mainHost'])) { // print "      ".$modx->context->key; return; } // print $host; $redir_host = ''; $redir_url = ''; //         switch($modx->event->name){ case 'OnPageNotFound': break; case 'OnHandleRequest': break; default: ; } //      if(!preg_match('/^'.$host.'$/i', $_SERVER['HTTP_HOST'])){ $redir_host = $host; $redir_url = $_SERVER['REQUEST_URI']; } //       ,   301-  (  ) if($redir_host || $redir_url){ $params = array(); $redir_url ? $params['url'] = $redir_url : ""; $redir_host ? $params['hostl'] = $redir_host : ""; $modx->runSnippet('redirect', $params); } 


Actually everything.

Source: https://habr.com/ru/post/151751/


All Articles