在哪里声明一个模块中不同包含文件的变量--Drupal [英] Where to declare variables common to different include files in a module - Drupal

查看:104
本文介绍了在哪里声明一个模块中不同包含文件的变量--Drupal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Drupal模块的例子。我将模块的管理和客户端功能分成两个include文件example.admin.inc和example.pages.inc。我在example.module文件中声明了常量(define()),它们可以在pages.inc文件和admin.inc文件中访问。



但是,如何在一个集中的位置声明正常的$变量,以便在admin.inc文件和pages.inc文件中都可以访问它们。我试图在example.module中声明$变量,但是在example.admin.inc和example.pages.inc中无法访问它。

解决方案

有多种可能性,具体取决于你想要的。



可覆盖的设置



你可以使用 variable_get('your_module_your_key',$ default_value); 。然后,在<。code> $ conf ['your_module_your_key'] ='other value'; 中的settings.php中可以很容易地覆盖它。请参阅 http://api.drupal.org/api/drupal /includes--bootstrap.inc/function/variable_get



还可以轻松构建一个设置表单,例如 http://api.drupal.org/api /drupal/modules--menu--menu.admin.inc/function/menu_configure/7



请注意,您应该始终将变量前缀到您的模块名称,并且 不要 将此变量频繁更改,因为每个更改导致所有变量的缓存清除。还要注意,每个页面请求都加载了所有变量,所以不要创建太多的变量或者存储大量的数据结构。它们主要用于配置。



静态获取/设置函数



您可以编写简单的get / set函数其在内部使用静态来在单个页面请求期间交换变量。参见例如 http://api.drupal.org /api/drupal/includes--path.inc/function/drupal_set_title/6



请记住,这些东西只能保存在一个页面上请求。这是一个示例实现,允许保存由字符串识别的多个变量,类似于variable_get()/ set()。

 <?php 
函数yourmodule_static($ key,$ value = NULL){
static $ storage;

if(isset($ value)){
$ storage [$ key] = $ value;
}
return $ storage [$ key];
}

//然后,你可以这样使用:
your_module_static('key',$ value);

//然后在另一个函数/文件中:
$ value = your_module_static('key');

您还可以扩展它以返回每个参考等等。



缓存



要存储例如数据慢数据库查询或复杂计算的数据,可以使用缓存系统。 http://www.lullabot.com/articles/a-beginners - 指向缓存数据看起来是一个详细和很好的解释。



请注意,缓存默认存储在数据库中,但是可插拔,例如也可以使用APC或Memcached。


I have a Drupal Module 'example'. I have split the admin and client side functionality of the module into two include files example.admin.inc and example.pages.inc. I declared constants (define()) in the example.module file, which are accessible in both the pages.inc file and admin.inc file.

But how can I declare normal $variables in a centralized location so that they can be accessible in both the admin.inc file and pages.inc file. I tried to declare $variable in example.module, but couldn't access it in the example.admin.inc and example.pages.inc.

解决方案

There are multiple possibilities, depending on want you want.

Overridable settings

You can use variable_get('your_module_your_key', $default_value);. Then, it is easy to override that either in settings.php with $conf['your_module_your_key'] = 'other value';. See http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variable_get.

It is also easy to build a settings form for that, see for example http://api.drupal.org/api/drupal/modules--menu--menu.admin.inc/function/menu_configure/7.

Note that you should always prefix your variables with your module name and that you don't use this for variables that change frequently because every change results in a cache clear for all variables. Also note that all variables are loaded on every page request, so don't create too many of them or store big data structures in it. They are mainly meant for configuration.

Static get/set functions

You can write simple get/set functions which use a static internally to exchange variables during a single page request. See for example http://api.drupal.org/api/drupal/includes--path.inc/function/drupal_set_title/6.

Keep in mind that these things are only kept for a single page request. Here is an example implementation which allows to save multiple variables identified by a string and therefor works similar like variable_get()/set().

<?php
function yourmodule_static($key, $value = NULL) {
  static $storage;

  if (isset($value)) {
    $storage[$key] = $value;
  }
  return $storage[$key];
}

// Then, you can use it like this:
your_module_static('key', $value);

// And then in a different function/file:
$value = your_module_static('key');

You could also extend it to return per reference and so on.

Cache

To store for example data from slow database queries or complex calculations, you can use the cache system. http://www.lullabot.com/articles/a-beginners-guide-to-caching-data looks like a detailed and great explanation of that.

Note that the cache is stored in the database by default but is pluggable and can for example also use APC or Memcached.

这篇关于在哪里声明一个模块中不同包含文件的变量--Drupal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆