We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 336
    • 6 Posts
    My customer is a member-based association and they want to provide a tool for their members to easily create a website. The high-level thought is to choose from several layout options, upload a logo, specify some other basic information and a new website is generated. Given Revo is capable of serving multiple domains from a single installation, I was wondering if it is possible to create a new context via the API? I couldn’t find any such capability in the Revo API documentation but I thought I’d ask.
      • 5340
      • 1,624 Posts
      Hi acatterson,
      and welcome to the MODx Forums.

      I am not yet an "expert" but here is how I think it should be done:

      Found this in one of the test cases and I think this is the correct way to create a context. Otherwise I suggest looking into modx_context and modx_context_setting tables

      //this could create a new context
      $ctx = $modx->newObject('modContext');
      $ctx->set('key','unittest');
      $ctx->set('description','The unit test context for context settings.');
      $ctx->save();


      next:
      - create context settings like base_url, etc
      - create/copy some files like core, config, htaccess into the domain/subdomain folder
      - create some documents into the new context and assign templates

      More information:
      http://rtfm.modx.com/display/revolution20/Contexts
      http://www.h-quadrat.com/techblog/111-modx-revolution-multi-domain-context-installation-english.php

      Hopefully this will put you on the right path until the experts have time to reply smiley
        • 22303 MODX Staff
        • 10,725 Posts
        Though you can create the table rows for the various objects like that, it might actually be better to use the processors for this purpose, e.g.
        <?php
        $_GET['key'] = 'unittest';
        $_GET['description'] = 'The unit test context for context settings.';
        $result = $modx->executeProcessor(array('action' => 'context/create'));
        ?>

        The processors provide a central location for consistent business logic to be used when the important core Revolution objects are being created. They provide validation, error messages that are localizable, call the core events that plugins are attached to, and more.

        There are certainly cases, where this is not the right solution, and you may want to manipulate the objects manually in your own process as cipa suggested.
          • 22303 MODX Staff
          • 10,725 Posts
          Previous post edited for inaccurate method call.

          Note that for 2.0.5, we have added a modX::runProcessor() method with more intuitive parameters and that does not require setting the input values for the processor as $_GET or $_POST variables. See commit details at GitHub.
            • 28215
            • 4,149 Posts
            An example of the 2.0.5 runProcessor:

            $result = $modx->runProcessor('context/get',array('key' => 'web'));
            if (empty($result['success'])) {
                 return 'Error occurred: '.$result['message'];
            }
            $context = $result['object'];
            $contextKey = $context['key'];
            /* etc */
            


            Or further:

            $result = $modx->runProcessor('context/create',array('key' => 'mynewcontext','description' => 'The new context!'));
            if (empty($result['success'])) {
                 return 'Error occurred creating context: '.$result['message'];
            }
            $newContext = $result['object'];
            $newContextKey = $context['key'];
            
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 336
              • 6 Posts
              Thank you all so much for your help. I’ll take a look at the suggestions and certainly learn a lot about Revo while I’m at it