【问题标题】:Joomla 3.x Get Module Parameters Within Ajax CallJoomla 3.x 在 Ajax 调用中获取模块参数
【发布时间】:2015-05-04 14:05:33
【问题描述】:

我正在使用 Joomla 3.x 中的自定义构建模块,它使用 Ajax 来显示一些实时数据。在 Ajax 调用执行的函数中,我需要访问模块参数,但我不确定如何执行此操作。通常,我可以将 $var = $params('paramName', 'default') 放在模块 PHP 文件中以获取参数,但是当由 Ajax 进行调用时,这不可用。这是我执行 Ajax 调用的模板代码:

<script type="text/javascript">
  jQuery(document).ready(function() {
    jQuery.get('index.php?option=com_ajax&module=whatsinport&method=getWhatsInPort&format=json', function(data) {
      console.log(data);
      var response = jQuery.parseJSON(data);

这是我的 helper.php 类中的代码:

class modWhatsInPortHelper {

  public static function getWhatsInPortAjax() 
  {
    $results = array();
    $results['status'] = 'ok';

    $app = JFactory::getApplication();
    $serverName = $app->getCfg('mod_whatsinport_serverName');
    $dbName = $app->getCfg('mod_whatsinport_dbName');
    $dbUser = $app->getCfg('mod_whatsinport_dbUser');
    $dbPwd = $app->getCfg('mod_whatsinport_dbPwd');

    $connectionInfo = array( "Database"=>$dbName, "UID"=>$dbUser, "PWD"=>$dbPwd, "ReturnDatesAsStrings"=>true);
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

$app->getCfg 似乎没有做我想做的事——我猜它只用于 Joomla 配置设置。我也试过了:

  $app = JFactory::getApplication();
  $params = $app->getParams();
  $serverName = $params->get('mod_whatsinport_serverName');
  $dbName = $params->get('mod_whatsinport_dbName');
  $dbUser = $params->get('mod_whatsinport_dbUser');
  $dbPwd = $params->get('mod_whatsinport_dbPwd');

但这也不起作用。我忘了包含我的模块配置文件:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>WhatsInPort</name>
    <author>Chris Krohn</author>
    <version>1.0.0</version>
    <description>Displays a list of current vessels in port.</description>
    <files>
        <filename>mod_whatsinport.xml</filename>
        <filename module="mod_whatsinport">mod_whatsinport.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
    </files>
    <config>
      <fields name="params">
        <fieldset name="basic">
          <field name="mod_whatsinport_serverName" type="text" default="" label="Server Name" description="NETBIOS name of the database server to connect to." />
          <field name="mod_whatsinport_dbName" type="text" default="" label="Database Name" description="Name of the database with the HMLOG table." />
          <field name="mod_whatsinport_dbUser" type="text" default="" label="Database User" description="User name to login to the server with." />
          <field name="mod_whatsinport_dbPwd" type="password" default="" label="Database Password" description="Password to login to the server with." />
        </fieldset>
      </fields>
    </config>
</extension>

【问题讨论】:

    标签: php ajax joomla


    【解决方案1】:

    想通了。应该是:

      $app = JFactory::getApplication();
      $module = JModuleHelper::getModule('mod_whatsinport','WhatsInPort');
      $params = new JRegistry($module->params);       
      $serverName = $params->get('mod_whatsinport_serverName');
      $dbName = $params->get('mod_whatsinport_dbName');
      $dbUser = $params->get('mod_whatsinport_dbUser');
      $dbPwd = $params->get('mod_whatsinport_dbPwd');
    

    【讨论】:

      【解决方案2】:

      getjax() 方法中检索参数的数据。

      public static  function getAjax(){
          $app = JFactory::getApplication();
          $module = JModuleHelper::getModule('my_module');
          $params = new JRegistry($module->params);
          return $params ->get('myparams');
      }
      

      【讨论】:

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