We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38740
    • 45 Posts
    Hi!
    I would like to host a few simple sites on one Modx installation Rev220. I thought i could do like this:

    1. I created a custom context.
    2. I made the Access Permissions the same as for the "web" context.
    3. I added a simple html resource.

    Opening a browser i entered the resource id (index.php?id=20) and expected my resource to show up. It didnt :-(

    Now i spent the day trying to figure out contexts, but it just seems terribly complicated. Or have i missed something?

    I would like to have users redirected to the appropriate Context based on "www.domain.com/customcontext".

    Can this be done without having multiple years of modx-experiance?
    I saw a plugin called ContextRouter but with no instructions. Dont understand how to use it.


    ...yes, im a newbie ;-)

    Be good!
    [ed. note: johnjohn22 last edited this post 11 years, 8 months ago.]
      ____________________________________________________
      • 3749
      • 24,544 Posts
      MODX routs you to the 'web' context by default in index.php, so resources in other context will be invisible.

      There are two basic methods for routing users to the correct context the way you want.

      Routing through index.php: http://rtfm.modx.com/display/revolution20/Creating+a+Subdomain+from+a+Folder+using+Virtual+Hosts


      Routing through with a "gateway" plugin: http://rtfm.modx.com/display/revolution20/Using+One+Gateway+Plugin+to+Manage+Multiple+Domains


      I think Mark Hamstra's ContextRouter will also do it, probably with less work on your part, but I've never used it. It's based on the second method above. If I understand his instructions, you set the context settings as described in the second link above, and then install ContextRouter and you're done.



      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 12 years, 1 month ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38740
        • 45 Posts
        Thx for your response Bob!

        But i cant get it to work.

        In the virtual host approach i dont understand how to set up the subdomains, do i create these at my service-providers control-panel or are thay only "virtual" in my modx installation? I have no acess to the web-server itself since i am "hosted".

        When i do all the settings without the web-server part something is different from before. Earlier i landed on the base-reseource of "web" but now i land on "cant find the page" when i access dev.domain.com. Cant figure out what doesnt function tho...

        If i now install ContextRouter the result is the same.


        Any idéas of whats wrong from this...?
          ____________________________________________________
          • 3749
          • 24,544 Posts
          It's hard to say at this point, but if you don't have access to the server, the other method (using index.php) may be a better choice.

          Be sure to add this to the <head> section of all templates:

          <base href="[[++site_url]]" />



          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODX info for everyone: http://bobsguides.com/modx.html

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 22427
            • 793 Posts
            Hi johnjohn22,

            you could try the following steps:

            (I assume that www.domain.com/ is the root of your main MODX installation, and you have a published start page "Home" in it.)

            (1)
            Install ContextRouter via the Package Management.

            (2)
            On the server, using some FTP Client, create a subdirectory /dev/ below the root directory.

            (3)
            Copy the files index.php and config.core.php from the root directory to this subdirectory /dev/.

            (4)
            Modify the file /dev/index.php in the following way:
            Near the end of the file you find the lines
            /* Initialize the default 'web' context */
            $modx->initialize('web');
            
            Replace them by
            /* Initialize the 'dev' context */
            $modx->initialize('dev');
            

            (5)
            In the file /dev/config.core.php, replace the line
            define('MODX_CORE_PATH', dirname(__FILE__) . '/core/');
            
            by the line
            define('MODX_CORE_PATH', dirname(dirname(__FILE__)) . '/core/');
            
            (Because from the subdirectory's "dev" point of view the core is to be found one level higher.)

            (6)
            In the MODX Manager, create the Context 'dev'.
            Create a resource "Dev-Home" under this context and publish it.

            (7)
            Go to the Context Settings panel of the Context 'dev' and create these settings:
            key: site_url  -> value: http:/ /www.domain.com/dev/   (omit the blank between / /)
            key: base_url  -> value: /
            key: http_host -> value: www.domain.com/dev
            key: site_start -> value: ##  (= the ID of the start page "Dev-Home" in the context 'dev')
            

            (8 )
            Change to the Context 'web', go to its Context Settings panel and create these settings:
            key: site_url  -> value: http:/ /www.domain.com/   (omit the blank between / /)
            key: base_url  -> value: /
            key: http_host -> value: www.domain.com
            key: site_start -> value: ##  (= the ID of the start page "Home" in the context 'web')
            


            That should do it.
            When you now (after a refresh of the cache) launch the URL www.domain.com/ you should see the page "Home", and when you launch the URL www.domain.com/dev/ you should see the page "Home-Dev". (Hope I'm right...)

            Good luck!



              • 38740
              • 45 Posts
              Thanks BobRay & Ottoga1 !

              I got it to work by:

              1. Entered a plugin with code:
              if ($modx->context->get('key') == 'mgr') {return;} 
              
              switch ($modx->getOption('http_host')) {
                  case 'site1.domain.com:
                      $modx->switchContext('site1');
                      break;
                  case 'site2.domain.com':
                      $modx->switchContext('site2');
                      break;
              
                  default:
                      // by default, don't do anything
                      break;
              


              2. Created two contexts, site1 & site2.

              3. Added "site_start" in the "Context Settings" pointing to starting resource for each context.

              4. I created the domain-aliases in my SP's controlpanel pointing to the default modx directory (same as for "web".

              5. In addition i have also created a subadmins user-group for each site1 & site2 with relevant access settings for the user of each site. Thx BobRay for tutorial!

              Now it works to my satisfaction.


              Question: Will this solution lead to any modx-functionality problems further ahead? Can i use wayfinder, getresources and other usefull functions freely on each site?

              :-)

              PS: By the way, this forum doesnt work so good in IE9, wellknown issue or...?... DS
                ____________________________________________________
                • 22427
                • 793 Posts
                What about Wayfinder: You can use the parameters &context and &startIdContext.
                So for example, a template for resources in context site1 could contain this Wayfinder call:
                [[!Wayfinder? &startId=`0` &context=`site2` &startIdContext=`22`]]
                That would output the resource tree of the children of the start page 22 in context site2.

                As far as I understand the code, Wayfinder temporarily switches to the other context to be able to access the resources from there.

                (Without the mentioned parameters, Wayfinder will catch just resources from the actual context. &startId=`0` lets start it at the root of the context. There is no access to resources of other contexts.) [ed. note: ottogal last edited this post 12 years, 1 month ago.]
                  • 38740
                  • 45 Posts
                  So far my above method seems to work quite nicely, i will report back if i run into problem.

                  Have discovered that getResources returns ALL resources no matter the context i'm in. But i could limit the resoruces by adding &context=`site1` to the call.

                  [[!getResources?
                     &context=`site1`
                     &limit=`0` 
                     &parents=`0` 
                     &sortby=`{"menuindex":"ASC"}`
                     &tpl=`tplFrontBtn`
                  ]]


                  By the way: Is there a global parameter-call i can make to get the name of active current Context?
                    ____________________________________________________
                    • 38740
                    • 45 Posts
                    I found it myself, i extract the context name by putting this code in a snippet:

                    echo $modx->context->key;
                      ____________________________________________________
                      • 3749
                      • 24,544 Posts
                      FYI, I think this would be slightly more correct:

                      return $modx->context->get('key');



                      ---------------------------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                      MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 12 years, 1 month ago.]
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting