How to include php file in drupal 7


How to include php file in drupal 7

我正在Drupal7中编写一个表单,我想"包含"一个php文件,该文件将我的DB连接变量添加到我的代码中。

我已经用几种方法测试了我的代码,我确信当我在我的网站上运行Cron时,"include"会让我获得WSOD。

我在谷歌上搜索过,并尝试过:

include("/sites/all/libraries/php/connection.inc");
include("./sites/all/libraries/php/connection.inc");
include"./sites/all/libraries/php/connection.inc";

我还尝试了上面的扩展名".php"。

我最后一次尝试:

define('__ROOT__', dirname(dirname(__FILE__)));
include(__ROOT__.'/sites/all/libraries/php/connection.inc');

有时我在尝试运行Cron时会收到WSOD,有时我提交表单时页面样式会被破坏。

在Drupal中手动包含PHP文件的正确方法是什么?请注意,我不想使用"Drupal方式"来实现这一点,也不想使用webform模块。我想用PHP手动编写代码。

libraries目录只能用于存储通过libraries模块作为库提供的文件(请参阅此处https://drupal.org/project/libraries)。

对于这两个示例,我们假设library.inc是我们的文件,relative_path_to是基于模块目录的library.inc文件的相对路径。

只包括一个文件,你可以使用:

require(drupal_get_path('module', 'module_name') . '/relative_path_to/library.inc');

用Drupal的方式(https://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7):

module_load_include('inc', 'module_name', '/relative_path_to/library');

干杯,j

尝试以下方法

1) require 'includes/iso.inc';
2) include_once  'includes/iso.inc';
3) include ('includes/iso.inc');

OR Drupal Ways

include_once DRUPAL_ROOT . '/includes/iso.inc';

在我看来,这些是正确的Drupal(7)包含代码的方法:

  1. module_load_include();函数-Drupal API文档-在需要时包括您或其他模块中的任何PHP文件
  2. your_module.info文件中的files[] = ...以自动加载包含您自己模块中的类和接口-编写.info文件文档

Libraries模块提供了自己的方式来包含库中的外部PHP文件。

第二个答案是正确的"Drupal方式"。如果事物的位置发生变化,被接受为答案的答案会有许多潜在的陷阱。使用drupal_get_path是最好的方法。如果您试图包含一个在模块更新挂钩期间可能未完全引导的文件,则情况尤其如此。