We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16429
    • 254 Posts
    Hi all!
    It’s pretty long that I don’t post in modx forum smiley

    I’m developing a module to manage reservations and I want to use Ajax to load some content.
    The php page that feeds the ajax response need to do some db work, and I want to use $modx functions to do this for various reason (mainly, not having to declare variables for dbname, dbuser, dbpassword etc).
    Since I cannot simply declare "global $modx" I think I have to include some document: can this get the job?
    include_once '../manager/includes/document.parser.class.inc.php';

    Or I have to instantiate a new $modx too?
    include_once '../manager/includes/document.parser.class.inc.php';
    $modx = new DocumentParser;

    Or I’m totally on the wrong way?


    Thanks all folks
      kudo
      www.kudolink.com - webdesign (surprised?)

      [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
      • 10487 MODX Staff
      • 1,535 Posts
      This is how AjaxSearch does it (just doing some work with AjaxSearch so that’s why I’m using as an example):
      // Setup the MODx API
      define('MODX_API_MODE', true);
      // initiate a new document parser
      include_once (MODX_MANAGER_PATH . '/includes/document.parser.class.inc.php');
      $modx = new DocumentParser;
      
      $modx->db->connect();
      $modx->getSettings();
      

      The MODX_API_MODE flag can be used to reduce the amount of work done on when PHP initialises the Document Parser so provides a small performance improvement - that’s assuming you wish to use it as an API of course (as opposed to a full-blown parser)
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 16429
        • 254 Posts
        Thx Garry
        I searched the AjaxSearch before posting because I thought it faced the same question, but I wasn’t able to find it.

        Glad my thought was right smiley
          kudo
          www.kudolink.com - webdesign (surprised?)

          [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
          • 16429
          • 254 Posts
          Finally I’ve got it. To use modX API this is the piece of code I’ve inserted in the script.
          Note that the actual path to manager includes is dependant on the path of the script itself. My was located in /assets/modules/Reservations/, so includes needs ../../../ in their path.
          Attention! This works, but I’m not sure it’s the perfect call. If someone knows, please correct it.
          	define('MODX_API_MODE', true);
          	include_once ('../../../manager/includes/config.inc.php');
          	require_once('../../../manager/includes/protect.inc.php');
          	include_once ('../../../manager/includes/document.parser.class.inc.php');
          	$modx = new DocumentParser;
          	$modx->db->connect();
          	$modx->loadExtension("ManagerAPI");
          	$modx->getSettings();
          
            kudo
            www.kudolink.com - webdesign (surprised?)

            [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
            • 7231
            • 4,205 Posts
            Just thinking out loud, I know nothing about this stuff...but I thought that all you needed was to declare "global $modx;" to bring the api into scope.
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don't know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 10449
              • 956 Posts
              I think it depends on how you build (include) your module:
              If you embed it via iframe, you have to do all that import/include stuff, because to PHP, it’s just a separate page (only visually "merged" in your browser).

              If you use PHP’s include(’myModule.php’) you don’t even need to declare global $modx - you are already inside the manager, and MODx does the magic for you.
                • 16429
                • 254 Posts
                Quote from: dev_cw at Apr 12, 2008, 09:44 AM

                Just thinking out loud, I know nothing about this stuff...but I thought that all you needed was to declare "global $modx;" to bring the api into scope.

                Ehm, no, because the page that serves the ajax response can’t simply "get the global $modx". This page is not loaded inside the manager (in that case, you simply need to call the global $modx to get it). The page is "alone", and to use $modx you need to include the class (and the config file, to get the link to the db).

                Quote from: ganeshXL at Apr 12, 2008, 09:59 AM

                I think it depends on how you build (include) your module:
                If you embed it via iframe, you have to do all that import/include stuff, because to PHP, it’s just a separate page (only visually "merged" in your browser).
                The page is not viewed inside the manager. Well, the module IS actually in the manager (as other modules) but when you do an ajax call you need a page that is not "inside" the manager. It’s only a php page, and if you don’t include the document parser class and the site config you can’t use the modx api at all. Obviously I may create a db link inside the page itself and retrieve information in a common php-mysql way, but I want to use the power of the modx framework, that’s what a framework is create to do.

                Quote from: ganeshXL at Apr 12, 2008, 09:59 AM

                If you use PHP’s include(’myModule.php’) you don’t even need to declare global $modx - you are already inside the manager, and MODx does the magic for you.
                This method is unsuitable for this situation because your module pages are in the manager but only when you load it in the manager. It’s the manager that eval()s your page, not your page that has a $modx = new DocumentParser. The manager takes care of giving your module the power of modx api at the cost of a "global modx" declaration. Basically, including your module page doesn’t get the api, because the link to the api is not in your page but in the manager itself.

                I don’t know if I was sufficiently clear smiley
                  kudo
                  www.kudolink.com - webdesign (surprised?)

                  [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  You can create a document with no template and just have a snippet call in it. The snippet can be nothing more than an include for your AJAX backend processor script. Use the page’s URL as the source for your AJAX response; it will be processed like any other page through the parser, and so will have access to the full API. TreasureChest does it this way.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 16429
                    • 254 Posts
                    Quote from: sottwell at Apr 12, 2008, 02:32 PM

                    You can create a document with no template and just have a snippet call in it. The snippet can be nothing more than an include for your AJAX backend processor script. Use the page’s URL as the source for your AJAX response; it will be processed like any other page through the parser, and so will have access to the full API. TreasureChest does it this way.

                    I’ve no words, this is way simpler than what I’ve thought about.
                    Thx Susan! You’re always enlightened wink
                      kudo
                      www.kudolink.com - webdesign (surprised?)

                      [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
                      • 27376
                      • 576 Posts
                      No, seriously, it’s like this:
                      <?php
                      // Set this to your MODx Base Path
                      $base_path = '/var/www/modx/';
                      
                      define('MODX_API_MODE', true);
                      include($base_path.'index.php');
                      $modx->db->connect();
                      $modx->getSettings();
                      
                      It’s that simple. Get a longer story here: http://modxcms.com/forums/index.php/topic,23556.0.html

                      This solution is for standalone pages/requests wanting to access MODx resources (Ajax Calls, External CMSs, etc.).