如何从joomla中的模块参数获取数据

时间:2013-04-11 20:14:21

标签: php joomla

我正在构建一个joomla 2.5模块,我想从模块参数访问数据传递,但是我得到以下错误:致命错误:在非对象上调用成员函数get() 我的代码如下:

helper.php

class modFeedGrabber
{
    function feedurl( $params ){
        return $params->get('feedUrl');
    }
}

mod_feedGrabber.php

defined( '_JEXEC' ) or die( 'Restricted access' );
$sitebase = JPATH_BASE;
$doc =& JFactory::getDocument();

// Include the syndicate functions only once
require_once( dirname(__FILE__).DS.'/helper.php' );
$feed = new modFeedGrabber();
$url = $feed->feedurl();
require( JModuleHelper::getLayoutPath( 'mod_feedGrabber' ) );

mod_feedGrabber.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" client="site" method="install">
    <name>Scroll Feed</name>
    <author>Josiah Gerald</author>
    <version>1.0.0</version>
    <date>03-04-2013</date>
    <description>A simple feed scroller.</description>
    <files>
        <filename>mod_feedGrabber.xml</filename>
        <filename module="mod_feedGrabber">mod_feedGrabber.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
        <folder>tmpl</folder>
    </files>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field
                    name="feedUrl"
                    type="url"
                    size="50"
                    label="Feed URL"
                    description="Enter the url of the feed" />


 </fieldset>
       </fields>
 </config>
</extension>  

如default.php

    <?php 
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<div id='feed' class="<?php echo $module_sfx; ?>"><?php echo $url; ?>
</div>

1 个答案:

答案 0 :(得分:1)

我可以看到提供的代码的主要问题是你没有将$params传递给你的帮助函数。它应该是这样的:

defined( '_JEXEC' ) or die( 'Restricted access' );
$sitebase = JPATH_BASE;
$doc =& JFactory::getDocument();

// Include the syndicate functions only once
require_once( dirname(__FILE__).DS.'/helper.php' );
$feed = new modFeedGrabber();
$url = $feed->feedurl($params);
require( JModuleHelper::getLayoutPath( 'mod_feedGrabber' ) );

此外,您的XML无效,因为它缺少fieldsetfields标记的结束标记。它应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" client="site" method="install">
    <name>Scroll Feed</name>
    <author>Josiah Gerald</author>
    <version>1.0.0</version>
    <date>03-04-2013</date>
    <description>A simple feed scroller.</description>
    <files>
        <filename>mod_feedGrabber.xml</filename>
        <filename module="mod_feedGrabber">mod_feedGrabber.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
        <folder>tmpl</folder>
    </files>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field
                    name="feedUrl"
                    type="url"
                    size="50"
                    label="Feed URL"
                    description="Enter the url of the feed"
                />
            </fieldset>
        </fields>
    </config>
</extension>

作为提示,您可以通过将XML文件拖入浏览器窗口来快速检查XML文件是否有效,例如:上面发布的代码结果如下:

Broken XML error in Chrome