We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5913
    • 42 Posts
    Hello All,

    I know I have done this before and it worked yet I cant get it to work again.

    I want to access the $modx->config parameters through a php file in my snippet directory and yet when I do the following below

    require_once ’../../../manager/includes/protect.inc.php’;
    include_once ("../../../manager/includes/config.inc.php");
    include_once(MODX_BASE_PATH."manager/includes/document.parser.class.inc.php");
    $modx = new DocumentParser;

    and then call print_r($modx->config); it returns an empty string. When I call $modx I get this
    DocumentParser Object ( [db] => DBAPI Object ( [conn] => [config] => Array ( [host] => ... [dbase] => `...` [user] => ... [pass] => ... [charset] => latin1 [connection_method] => SET CHARACTER SET [table_prefix] => modx_ ) [isConnected] => [_dbconnectionmethod] => SET CHARACTER SET [dataTypes] => Array ( [numeric] => Array ( [0] => INT [1] => INTEGER [2] => TINYINT [3] => BOOLEAN [4] => DECIMAL [5] => DEC [6] => NUMERIC [7] => FLOAT [8] => DOUBLE PRECISION [9] => REAL [10] => SMALLINT [11] => MEDIUMINT [12] => BIGINT [13] => BIT ) [string] => Array ( [0] => CHAR [1] => VARCHAR [2] => BINARY [3] => VARBINARY [4] => TINYBLOB [5] => BLOB [6] => MEDIUMBLOB [7] => LONGBLOB [8] => TINYTEXT [9] => TEXT [10] => MEDIUMTEXT [11] => LONGTEXT [12] => ENUM [13] => SET ) [date] => Array ( [0] => DATE [1] => DATETIME [2] => TIMESTAMP [3] => TIME [4] => YEAR ) ) ) [event] => SystemEvent Object ( [name] => [_propagate] => 1 [_output] => [activated] => [activePlugin] => ) [Event] => SystemEvent Object ( [name] => [_propagate] => 1 [_output] => [activated] => [activePlugin] => ) [pluginEvent] => Array ( ) [config] => [rs] => [result] => [sql] => [table_prefix] => [debug] => [documentIdentifier] => [documentMethod] => [documentGenerated] => [documentContent] => [tstart] => [minParserPasses] => [maxParserPasses] => [documentObject] => [templateObject] => [snippetObjects] => [stopOnNotice] => [executedQueries] => [queryTime] => [currentSnippet] => [documentName] => [aliases] => [visitor] => [entrypage] => [documentListing] => [dumpSnippets] => [chunkCache] => [snippetCache] => [contentTypes] => [dumpSQL] => [queryCode] => [virtualDir] => [placeholders] => [sjscripts] => Array ( ) [jscripts] => Array ( ) [loadedjscripts] => Array ( ) [documentMap] => [forwards] => 3 [dbConfig] => Array ( [host] => ... [dbase] => `...` [user] => ... [pass] => ... [charset] => latin1 [connection_method] => SET CHARACTER SET [table_prefix] => modx_ ) ) 


    Obviously I have removed db info and replaced with ... smiley

    Any ideas as to why I am not accessing the $modx?
      • 11055 ☆ A M B ☆
      • 3,112 Posts
      <?php 
      function moo() {
        global $modx;
      
      }
      


      that’s all you need.
        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.
        • 3749
        • 24,544 Posts
        The modxapi below is handy, just include it and then call:

        $path = 'path/to/api/file';
        include_once($path.'modxapi.php');
        
        $modx = new MODxAPI();
        // connect to db and load settings
        
        $modx->connect();
        $modx->getSettings();
        

        <?php
        /**
         * MODx API Library 
         * Use this api to connect to the MODx Content Managment System
         * 
         * @author: Raymond Irving, 2006
         *
         */
         
         
        $_api_path = dirname(__FILE__).'/';
        
        // include config file
        include_once($_api_path.'manager/includes/config.inc.php');
        
        // include modx parser file
        include_once($base_path.'manager/includes/document.parser.class.inc.php');
        
        class MODxAPI extends DocumentParser {
        
        	function MODxAPI() {
        	
        		$this->startSession();
        		parent::DocumentParser();
        		
        		// set some parser options
        		$this->minParserPasses = 1;		// min number of parser recursive loops or passes
        		$this->maxParserPasses = 10;	// max number of parser recursive loops or passes
        		$this->dumpSQL = false;
        		$this->dumpSnippets = false;
        		
        		// set start time
        		$this->tstart = $this->getMicroTime();	// feed the parser the execution start time	
        
        	}
        
        	// execute parser and return results - To be finalized
        	function executeDocument($docid = 0) {
        		ob_start();
        			ob_start();
        				$tmp = $_REQUEST['id']; // save old id
        				$_REQUEST['id'] = $docid;
        				$this->executeParser();
        		$html = ob_get_contents();
        		ob_end_clean();
        		$_REQUEST['id'] = $tmp; // restore old id
        		return $html;		
        	}
        	
        	// connect to MODx database - use $modx->db->query();
        	function connect() {
        		 $this->db->connect();
        	}
        	
        	function startSession() {
        		startCMSSession();
        	}		
        }
        
        ?>
          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
          • 5913
          • 42 Posts
          Thanks BobRay,

          that worked perfectly after a few modifications.

          I had to change the following in the modxapi.php file

          //make sure this file is in manager/includes.

          // include config file
          include_once(’config.inc.php’);

          // include modx parser file
          include_once(’document.parser.class.inc.php’);

          by doing so I guaranteed it working on all servers.

          But what I don’t understand is why they way I have been doing it no longer works?
            • 11055 ☆ A M B ☆
            • 3,112 Posts
            why do you make more when you can do less?
            I don’t know what you’re up to, but here are some examples on using available MODx API on the fly

            <?php
            
            function moo() {
              global $modx;
            
              // connect db
              // actually you don't have to use this, MODx always starts a connection when parsing its document,
              // and shuts down after finish.
              $modx->db->connect;
            
              // want to get document?
              $modx->documentName;
              $modx->documentContent;
              $modx->documentObject;
              // etc...
            
              // more advance, retrieving value from your custom DB?
              // untested, but I had used this sort of like this
              $row=$modx->getRow($modx->query('SELECT/INSERT/UPDATE/DELETE queries'),assoc);
              //let's say...
              if ($row['username']==$_POST['username'] {
                echo 'Hello '.$row['username'];
              }
              // else denied?
              
            }
            


            If you’re working on EVO, grab the API cheatsheet from my signature.
              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.
              • 5913
              • 42 Posts
              I am unable to call global $modx from outside of MODx. I was trying to access the $MODx framework from a php file and calling global $modx just wont work.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                $path = ’path/to/api/file’;
                include_once($path.’modxapi.php’);

                $modx = new MODxAPI();
                  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