php 如何在drupal 7中包含php文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23047315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:27:04  来源:igfitidea点击:

How to include php file in drupal 7

phpdrupaldrupal-7cronphp-include

提问by donbuche

I'm coding a form in Drupal 7, and I want to 'include' a php file that adds my DB connection variables to my code.

我正在 Drupal 7 中编写表单,并且我想“包含”一个 php 文件,该文件将我的数据库连接变量添加到我的代码中。

I've tested my code in several ways, and I'm sure that the 'include' makes me get a WSOD when I run Cron in my site.

我已经以多种方式测试了我的代码,并且我确信当我在我的站点中运行 Cron 时,'include' 会让我得到一个 WSOD。

I've Googled about this and I tried:

我在谷歌上搜索过这个,我试过:

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

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

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

I also tried the above with '.php' extension.

我也试过上面的'.php'扩展名。

And my last try:

我最后一次尝试:

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

Sometimes I get a WSOD when trying to run Cron, and sometimes my page styles get broken when I submit a form.

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

What is the correct way to include a PHP file manually in Drupal? Note that I don't want to use the 'Drupal way' to do this or to use the webform module. I want to code it manually with PHP.

在 Drupal 中手动包含 PHP 文件的正确方法是什么?请注意,我不想使用“Drupal 方式”来执行此操作或使用 webform 模块。我想用PHP手动编码。

回答by schlicki

The libraries directory should only be used to store files shipped as a library through the libraries module (see here https://drupal.org/project/libraries).

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

For both examples lets assume library.inc is our file and relative_path_to is the relative path based on the module directory to our library.inc file.

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

To just include a file you can use:

要仅包含一个文件,您可以使用:

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

And to do it the Drupal way (https://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7):

并使用 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');

Cheers, j

干杯,j

回答by Mehul Jethloja

Try this ways

试试这些方法

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

OR Drupal Ways

或 Drupal 方式

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

回答by David Lukac

In my opinion these are correct Drupal (7)ways to include code:

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

  1. module_load_include();function - Drupal API documentation- to include any PHP files from within your or other modules where needed,
  2. files[] = ...in your_module.infofile to autoload include classes and interfaces within your own module - Writing .info filedocumentation.
  1. module_load_include();功能 - Drupal API 文档- 在需要的地方包含您或其他模块中的任何 PHP 文件,
  2. files[] = ...your_module.info文件中自动加载包含您自己模块中的类和接口 -编写 .info 文件文档。

Libraries module provides it's own way to include external PHP files from the libraries.

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

回答by ElusiveMind

The second answer is the correct "Drupal way". The one that was accepted as the answer has many potential pitfalls if locations of things change. Using drupal_get_path is the best way. This is also especially true if you are trying to include a file that may not be fully bootstrapped during a module update hook.

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