-
- 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.
-
- 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.
-
- 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;
-
- 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.