We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36516
    • 179 Posts
    This seems to be an unusual case, but I'm attempting to make Modx play nicely with a second web app, in this case Open Cart, in order to allow me to have Modx manage the main site, but then operate Open Cart for a shop section of the site, all the while displaying things like the OC category list on Modx pages (done with script that taps directly into the OC DB tables), and visa versa with OC pages displaying Modx menu(s) in various places within the site template.

    All I really need is the raw output from a very simple Wayfinder call:

    [[Wayfinder? &startId=`130`]]


    So I've researched how to load Modx externally, and came up with this:

    require_once '../config.core.php';
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    $modx = new modX();
    $modx->initialize('web');


    ...which has worked to allow me something unrelated; access to the Modx session variables to allow Modx pages to track cart contents. For this part I've simply added:

    echo $modx->runSnippet('Wayfinder', array('startId' => 130));


    ...but this produces the error:

    Call to a member function get() on a non-object


    ...because in the Wayfinder class the constructor doesn't appear to be aware of the $modx object that was used to set it off in the first place, and can't retrieve a resource ID. Frustratingly, while I'm sure there would be more issues further down if I was to change the code in the Wayfinder class, this particular call to Wayfinder doesn't actually need a resource ID and so that particular operation could be ignored.

    The conclusion I'm drawing is that while there's potential for this kind of operation from an external script, Wayfinder specifically isn't engineered to be called like this. I wonder if anyone else has any bright ideas about how to get parts of a modx site to render within another script on the same site.

    This question has been answered by BobRay. See the first response.

      • 11055 ☆ A M B ☆
      • 3,112 Posts
      Quote from: davidsmith at May 19, 2016, 11:07 PM

      So I've researched how to load Modx externally, and came up with this:

      require_once '../config.core.php';
      require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
      $modx = new modX();
      $modx->initialize('web');


      Replace this with:
      define('MODX_API_MODE', true);
      include_once 'index.php'; // your MODX's main index.php
      

      $modx will be entirely the same object with the running session, including $modx->user object.
        Rico
        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
        MODx is great, but knowing how to use it well makes it perfect!

        www.virtudraft.com

        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

        Maintainter/contributor of Babel

        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
        • 36516
        • 179 Posts
        Hi, and thanks for the quick response. Sounds like that's my best shot so I'll try it immediately. However, I've come across this page:

        https://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/loading-modx-externally

        ...on which it gives the following code similar to yours:

        define('MODX_API_MODE', true);
         
        // Full path to the index
        require_once('/path/to/modx/public_html/index.php');
        $modx = new modX();
        $modx->initialize('mgr');
        


        They seem to suggest that a new instance of modx is still required, even though index.php clearly creates one. I'm still unsure about scope in these cases; whether or not I need to declare "global $modx" depending on where I plan to use it across my site, but for now I suppose I just need to decide if it's really necessary to create a new instance or just use the one I get free with index.php. Your code suggest the latter.
          • 36516
          • 179 Posts
          I've tried it in API mode and unfortunately it makes no difference. It seems Wayfinder's class file doesn't have access to the $modx object through runSnippet(), though I can't imagine why.
            • 11055 ☆ A M B ☆
            • 3,112 Posts
            Would you share more complete script of yours?
              Rico
              Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
              MODx is great, but knowing how to use it well makes it perfect!

              www.virtudraft.com

              Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

              Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

              Maintainter/contributor of Babel

              Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
              • 36516
              • 179 Posts
              Apologies for the delay. There's not really a great deal more to share. I've got Modx in the root of my site, and Open Cart in the "/shop" subfolder. In OC's index.php I have the following:

              define('MODX_API_MODE', true);
              include_once '../index.php';


              ...which includes the Modx index.php from the site root, though I note now that the URL is relative and hope that doesn't have some unforeseen effect.

              Then, in one of my OC template files, I simply use:

              global $modx;
              echo $modx->runSnippet('Wayfinder', array('startId' => 130));


              I'm unsure if I need to use "global", but I seem to remember that without it the error log produces errors that suggest "runSnippet" isn't a value member function, but with it the error log complains about the lack of access to the $modx object from within runSnippet, implying that it is necessary.

              If there's more/other code you think it would be useful to see I'll gladly produce it, but I can't imagine what else that might be. My requirements are rather basic, I just don't understand why the $modx object isn't in scope from within the runSnippet call to Wayfinder.

              Thanks again for your help, and interest.
                • 36516
                • 179 Posts
                I'm unsure if

                $modx->initialize('web');


                is going to be necessary, and potentially the cause of my problem. But it's included in the root index.php, right? So I'm getting that with the include anyway.
                • discuss.answer
                  • 3749
                  • 24,544 Posts
                    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
                    • 36516
                    • 179 Posts
                    Thanks Bob, that looks like just what I need, and very comprehensive. I'm curious though. Your guide doesn't mention anything about API Mode. Does that imply there's no need to use it? I'll give your instance code a shot either way and report back.
                      • 36516
                      • 179 Posts
                      Thanks again, Bob. Your guide has made it clear now that the reason Wayfinder fails is that it is dependent on receipt of a resource ID which I've needed to configure, even though my call doesn't really make use of it. Now my call is working and my modx-managed menus are appearing in OpenCart, which is fabulous!

                      Thanks slso to goldsky. I've been looking forward for some time to the day I'd discover how to integrate Modx into other apps, and render specific parts of my sites within adjoined apps like OpenCart, and I've finally had the time and inclination to look into it properly. Thanks to you guys I've hit a milestone in my understand of Modx which will enable me to construct much more manageable sites in future.