We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 54674
    • 3 Posts
    I have parts in my snippets that I need to be more like global functions/variables, as they are often reused without change a lot. I have been trying to simply use include or require, but it always causes my pages to just error out and become unusable.

    I made a directory in the base directory called 'phpSnips' with 'file.php'. but all directory forms I used failed- such as '../../phpSnips/file.php' and '/phpSnips/file.php'. How do I start the directory from filesystem to be able to include the file, or is there a better way without using another snippet and returning a string to turn into an object somehow.
      • 42562
      • 1,145 Posts
      Just as you would in PHP, the FULL server path

      include '/server/username/public_html/myfolder/file.php';
      
      include MODX_BASE_PATH . 'myfolder/file.php';
      
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
        • 3749
        • 24,544 Posts
        Another popular way to go would be to put those methods in a PHP class, then just include the class file, instantiate the class object, and call its methods. The class file will get cached and the include will be very fast.

        You can also use MODX System Settings as global variables (or $_SESSION variables if they're temporary). For System Settings (after creating the settings):

        /* To get one */
        $value = $modx->getOption('my_setting', null, 'default value (optional)');
        
        /* To set one */
        $setting = $modx->getObject('modSystemSetting', array('key' => 'my_setting'));
        $setting->set('value', $someValue);
        


        /* Session Variable alternative method */
        
        /* Get one */
        $value = $_SESSION['my_setting'];
        
        /* Set one */
        $_SESSION['my_setting'] = $someValue;
        
        


          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
          • 54674
          • 3 Posts
          Thanks for your help, but I eventually found the solution of using :
          include $_SERVER['DOCUMENT_ROOT']. "/phpSnips/serverConnect.php";


          which got the results I was looking for (although not the cleanest and shortest probably), which works the easiest for me personally- though I can't judge if it was the best solution due to being self taught in PHP and being brand new to ModX.
            • 3749
            • 24,544 Posts
            Using the one of the MODX constants might be slightly more reliable and portable:

            MODX_BASE_PATH
            MODX_MANAGER_PATH
            MODX_CORE_PATH
            MODX_ASSETS_PATH

            Note that all of them end in a slash, so make sure the rest of the path doesn't begin with one.



              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