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}を呼び出すと、未定義の変数uaが返されます。

最終的には、Detectorライブラリが返す変数ごとに異なる関数を設定する予定はありませんが、現時点では、何かを出力するようにしようとしています。

私はもともと、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 に答える