We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23018
    • 353 Posts
    I have followed XXXX instruction on how to setup a multi site context

    http://www.h-quadrat.com/techblog/111-modx-revolution-multi-domain-context-installation-english.php

    To make it work. I had to add some code to index.php
    
    //$modx->initialize('web');
    
    /* setup for multiple domains */
    switch(strtolower(MODX_HTTP_HOST)) {
       case 'www.modx-templates.com:80':
       case 'www.modx-templates.com':
          // if the http_host is modx-templates.com, switch the context to web
          $modx->initialize('web');
          break;
       case 'www.ffmwireless.de:80':
       case 'www.ffmwireless.de':
          // if the http_host is ffmwireless.de, switch the context to ffmwireless
          $modx->initialize('ffmwireless');
          break;
       case 'www.thesearchgoeson.com:80':
       case 'www.thesearchgoeson.com':
          // if the http_host is thesearchgoeson.com, switch the context to thesearch
          $modx->initialize('thesearch');
          break;
      default:
          // by default / if no other rule can be applied, load 'web' context
          $modx->initialize('web');
          break;
    }
    


    This works very nice, but I’m wondering if there isn’t a more straightforword method to to this.

    As context names I have used Domain URLs:
    www.thesearchgoeson.com
    www.ffmwireless.de

    All I need to know are the available Contexts. Then I get rid of ’mgr’ and ’web’ and voila I have a list of all available domains

    As far as I understand there is a list of all contexts in the database: modx_context -> ’id’.

    OK, I can make a simple db query, but from what I have learned from the API it should be dead simple to get an array with all available contexts by utilizing xPDO.

    Questions:

    • I’m a total xPDO noob. Can you give me any advice how to do that?
    • Do you see any performance problems with this approach?
    • Do you have other ideas how to do this automacically without touching index.php everytime I add a new context?
    • Is there a way to do this without touching index.php?

    I really would appreciate any input.

    Kind regards,

    pepebe
      Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
      • 21257 MODX Staff
      • 730 Posts
      There’s no "right place" to do the context switching, but many often do it in a plugin. I have a plugin that maps subdomain names on incoming requests to contexts.
      See notes for shortcomings and where this plugin could potentially be improved.
      https://gist.github.com/739044
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 36764 ☆ A M B ☆
        • 45 Posts
        Evengard aka Eiventeleiron Reply #3, 15 years, 5 months ago
        Registered just to answer that question (even if it is a bit later).

        I searched the whole MODx source tree to get the answer, which is that:
        in file ./core/model/modx/processors/context/getlist.php we get the algorithm:
        $contexts = $modx->getCollection(’modContext’);

        So yes, this is logical if we think in xPDO terms, but not straightforward if we don’t.
          • 22303 MODX Staff
          • 10,725 Posts
          This depends. If you are looking to automate this in index.php, you would need to first decide if you wanted this query to respect Access Control and if so, you would need to make sure anonymous users had permission to load all Contexts. If so, you can use
          <?php
          $contextKeys = array();
          $contexts = $modx->getCollection('modContext', array('key:NOT IN' => array('web', 'mgr')));
          foreach ($contexts as $context) {
              // do stuff with each modContext object
              $contextKeys[] = $context->get('key');
          }
          


          Otherwise, you would want to bypass the objects and simply pull the key column using a query you create using the API:
          <?php
          $contextKeys = array();
          $query = $modx->newQuery('modContext', array('key:NOT IN' => array('web', 'mgr')));
          $query->select($modx->getSelectColumns('modContext', 'modContext', '', array('key')));
          if ($query->prepare() && $query->stmt->execute()) {
              $contextKeys = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
          }
          

          This approach would avoid the overhead of access permission checks and of hydrating instances of the table classes, since it is simply returning a PDO result set object. [ed. note: opengeek last edited this post 13 years, 1 month ago.]
            • 44843 ☆ A M B ☆
            • 10 Posts
            Just updating for future reference as i've just used this. there wasa syntax error on line 3.

            <?php
            $contextKeys = array();
            $query = $modx->newQuery('modContext', array('key:NOT IN' => array('web', 'mgr')));
            $query->select($modx->getSelectColumns('modContext', 'modContext', '', array('key')));
            if ($query->prepare() && $query->stmt->execute()) {
                $contextKeys = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
            }