【问题标题】:Way to access ModX database by including ModX core file?通过包含 ModX 核心文件来访问 ModX 数据库的方法?
【发布时间】:2015-08-04 05:48:07
【问题描述】:

我有一个 PHP 文件(我无法从中生成 sn-p)。我需要将它连接到数据库,而不是按照正常方式进行操作,我想知道我是否可以只包含某种已经具有数据库连接的头文件。

有人知道Modx文件结构中有这样的文件吗?

【问题讨论】:

    标签: modx modx-revolution


    【解决方案1】:

    简单...

    如果你打开主index.php,你可以看到一些提示:

    /* define this as true in another entry file, then include this file to simply access the API
     * without executing the MODX request handler */
    if (!defined('MODX_API_MODE')) {
        define('MODX_API_MODE', false);
    }
    
    // ...
    
    /* execute the request handler */
    if (!MODX_API_MODE) {
        $modx->handleRequest();
    }
    

    也就是说,如果你有一个原始的 PHP 文件,让我们举个例子:hello.php

    <?php
    
    define('MODX_API_MODE', true); // IMPORTANT!!!
    require 'index.php'; // or give directory path, according to your need
    
    // let's test it
    $startPage = $modx->getObject('modResource', $modx->getOption('site_start', null, 1));
    
    if (!$startPage) {
        die('CRAP!');
    }
    
    $startPageArray = $startPage->toArray();
    echo '<pre>';
    print_r($startPageArray);
    echo '</pre>';
    die('WOOHOO');
    

    不,您不必再次定义$modx

    它使用index.php中的同一个对象。

    【讨论】:

      【解决方案2】:

      如果你在外部脚本中加载 modx 模块,你可以对你的 modx 数据库运行任何操作 [事实上你可以使用任何 modx 函数]

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

      一旦你实例化了 modx 对象,它就会处理你所有的数据库连接细节。这适用于任何页面,而不仅仅是管理器页面。

      【讨论】:

        【解决方案3】:

        允许使用系统数据库配置进行数据库连接的唯一地方是管理器页面,但这需要更多的工作来编写插件来访问其类和功能。

        如果你希望能够使用 MODX 功能建立连接,我建议使用它的 xPDO 来执行查询,至少出于安全原因。

        这样的设置是:

        define('MODX_CORE_PATH', '/path/to/revo/core/'); 定义('MODX_CONFIG_KEY','配置'); 需要一次 MODX_CORE_PATH 。 '模型/modx/modx.class.php';

        $host = '本地主机'; $username = '你的用户名'; $password = '你的密码'; $dbname = '你的数据库'; $端口 = 3306; $charset = 'utf-8';

        $dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset"; $xpdo = new xPDO($dsn, $username, $password);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多