2

どのページでも実行できる単純な関数を作成しようとしています。

function something() {
    $string = 'Hello World'; 
    return $string;
}

私がカテゴリページにいるとしましょう。電話$a = something();をかけるだけで、値が返されます

プラットフォーム : OpenCart

PS私はまだMVCアーキテクチャを研究しています

4

4 に答える 4

7

MVC システムについて理解し、学びたいので、正しい方法は、独自のヘルパー ファイルを作成して に配置し、/system/helper/そのヘルパーを に追加することsystem/startup.phpです。ガイドとしてこれらでjson.php/がどのように行われるかを見てくださいutf8.php

于 2013-07-02T10:39:12.210 に答える
3

Create a new file to system/library/yourclassname.php with same code.

Also please add a class name to your function as below:

class yourclassname {

  function something() {
    $string = 'Hello World'; 
    return $string;
  }
}

And add it to your index.php file as below;

$registry->set('yourclassname', new yourclassname($registry));

Finally add it to your startup.php file as below:

require_once(DIR_SYSTEM . 'library/yourclassname.php');

You can use it anywhere with $this->yourclassname->something();

Thats All..

于 2014-07-25T14:19:03.783 に答える
1

任意の opencart ライブラリ (system/library/) で関数を作成できます。

system/library/document.php の例として

    function something() {
    $string = 'Hello World'; 
    return $string;
}

そして、openсartのどこでも次のように使用します

$something=$this->document->something();

header.tpl の P/s コードは ajax または直接要求では機能しません

于 2013-07-02T20:52:52.077 に答える
0

マトリコアの答えは私にとっては完璧に機能しましたが、利用可能な構成とデータベースを使用して新しいクラスを構築する必要もありました。

以下のコード:

class yourclassname {

    public function __construct($registry) {
        $this->config = $registry->get('config');
        $this->db = $registry->get('db');
    }

}

$this->db->query("Your query here"); 経由でクエリを実行できます。

于 2014-10-12T11:28:18.883 に答える