【问题标题】:Joomla 3 $params->get returns NULL values in moduleJoomla 3 $params->get 在模块中返回 NULL 值
【发布时间】:2016-08-10 01:09:04
【问题描述】:

我正在为 Joomla 编写一个新模块! 3 我被 $params->get 难住了。

我可以在 modules 表中看到数据库中的值,但是以下代码对于已设置的每个参数都没有返回任何内容,包括 module_tag、bootstrap_size、header_tag、header_class、style 的默认参数。

代码是:

$app = JFactory::getApplication();
$params = $app->getParams();
$param = $params->get('module_tag');

查看变量 $param 的类型时,该类型被重新调整为 NULL。

Joomla! 3.6.2
稳定的 PHP 5.6.18
MySQL 5.6.31

【问题讨论】:

    标签: php joomla joomla3.0


    【解决方案1】:

    您必须指定要检索参数的模块名称。将 'moduleName' 替换为您的模块名称。

    $app = JFactory::getApplication();
    $params = $app->getParams('moduleName');
    $param = $params->get('module_tag');
    

    【讨论】:

    • 请解释你做了什么以及为什么它应该解决 OP 的问题?
    • 感谢您的回复。我已经尝试过了,尽管文档说如果从模块内调用没有必要。不幸的是,它不起作用。
    • 你能和我分享你的模块,以便我调试问题吗?
    【解决方案2】:

    在获取参数之前需要注意几点:

    一个。命名约定 模块开发的标准模式中使用了四个基本文件:

    mod_helloworld.php - This file is the main entry point for the module. 
    mod_helloworld.xml - This file specifies configuration parameters for the module.
    helper.php - This file do the actual work in retrieving the information  (usually from the database or some other source).
    tmpl/default.php - This is the module template. This file will take the data collected by mod_helloworld.php and generate the HTML to be displayed on the page.
    

    检查 mod_helloworld.php 和 mod_helloworld.xml 名称是否相同。

    b.您的参数只有在您保存过一次时才会显示。

    在跳转检索参数之前检查此页面https://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module

    有两种选择

    1. 当从模块内部调用时,在你的 mod_yourmodule.php 文件中使用它

      $module_tag = $params->get('module_tag', '默认值');

    然后你可以直接在你的tmpl->default.php文件中调用$module_tag

    1. 当从模块外的任何地方调用时,使用 this

      jimport('joomla.application.module.helper');

      $module = JModuleHelper::getModule('yourmodule');

      $moduleParams = new JRegistry($module->params);

      $module_tag = $moduleParams ['module_tag'];

      jimport('joomla.html.parameter');

      jimport('joomla.application.module.helper');

      $module = JModuleHelper::getModule('yourmodule');

      $moduleParams = new JRegistry();

      $moduleParams->loadString($module->params);

      $module_tag = $moduleParams->get('module_tag');

    【讨论】:

    • 感谢您的回复,但我不确定这是如何解决问题的。在 1 中,您所做的只是给它一个默认值。我不想要默认值,我想要已经保存在首选项中的值。和 2 不相关,因为我在模块中调用它
    • 尝试理解两者的目的。第一个没有给出默认值,但返回已经保存的那个。如果无法仅获取保存的值,则返回默认值。如果您从 helper.php 文件中调用,在您自己的模块本身中,第一个可能不起作用,但第二个始终有效。从这一切来看,我认为您没有遵循命名约定。可能在其他地方犯了小错误。
    • 感谢您再次抽出宝贵时间。我的理解足以理解命令的作用。但问题是 $params->get 没有从 eh 数据库中获取值。该命令所做的只是返回默认值。而且我已经在其他领域编程超过 25 年,所以是的,我确实遵循了文件的命名约定。代码 sn-p 是我为获取初始参数所做的测试,并且在所有变体中 $params->get 都无法返回存储的值。
    • 你能把模块上传到某个地方让我检查一下吗?某些文件可能有错误。使用免费上传服务smashingapps.com/2009/05/22/… 并在此处粘贴链接
    猜你喜欢
    • 2023-01-13
    • 1970-01-01
    • 2016-12-07
    • 2016-10-11
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多