1

我正在开发一个基于外部库进行设备检测的插件。

这是我到目前为止所拥有的:

class Deetector {

// public $return_data;

/**
 * Constructor
 */
public function __construct()
{
    $this->EE =& get_instance();
    $this->EE->load->add_package_path(PATH_THIRD.'/deetector');
    $this->EE->load->library('detector');
    $this->return_data = "";
}

public function deetector()
{
    return $ua->ua;
}

public function user_agent()
{
    return $ua->ua;
}

// ----------------------------------------------------------------

/**
 * Plugin Usage
 */
public static function usage()
{
    ob_start();

    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
}
}

如果我调用 {exp:deetector} 我在模板中没有输出。如果我调用 {exp:deetector:user_agent} 我得到Undefined variable: ua

最终,我不打算为 Detector 库返回的每个变量设置不同的函数,但我只是想让它现在输出一些东西。

我最初是作为一个扩展开始这样做的,它将检测器库的变量添加到全局变量数组中,并且工作正常;只是自从尝试将其作为插件进行操作后,我才遇到了问题。

4

1 回答 1

2

你还没有设置$this->ua任何东西。我假设它是您加载的检测器库的变量,因此您可能想要执行以下操作:

class Deetector {
    public function __construct()
    {
        $this->EE =& get_instance();

        // remove this line, it's probably not doing anything
        // $this->EE->load->add_package_path(PATH_THIRD.'/deetector');

        $this->EE->load->library('detector');

        // note you use $this->return_data instead of "return blah" in the constructor
        $this->return_data = $this->EE->detector->ua;
    }

    // remove this, you can't have both __construct() and deetector(), they mean the same thing
    // public function deetector()
    // {
    //     return $ua->ua;
    // }

    public function user_agent()
    {
        return $this->EE->detector->ua;
    }
}

更新:

我查看了Detector docs,它不遵循正常的库约定(它在包含文件时定义了 $ua 变量)。出于这个原因,您应该忽略标准的 EE 加载函数,并直接包含该文件:

class Deetector {
    public function __construct()
    {
        $this->EE =& get_instance();

        // manually include the detector library
        include(PATH_THIRD.'/deetector/libraries/detector.php');

        // save the local $ua variable so we can use it in other functions further down
        $this->ua = $ua;

        // output the user agent in our template
        $this->return_data = $this->ua->ua;
    }
}
于 2012-10-26T23:09:14.043 回答