どのページでも実行できる単純な関数を作成しようとしています。
function something() {
$string = 'Hello World';
return $string;
}
私がカテゴリページにいるとしましょう。電話$a = something();
をかけるだけで、値が返されます
プラットフォーム : OpenCart
PS私はまだMVCアーキテクチャを研究しています
MVC システムについて理解し、学びたいので、正しい方法は、独自のヘルパー ファイルを作成して に配置し、/system/helper/
そのヘルパーを に追加することsystem/startup.php
です。ガイドとしてこれらでjson.php
/がどのように行われるかを見てくださいutf8.php
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..
任意の 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 または直接要求では機能しません
マトリコアの答えは私にとっては完璧に機能しましたが、利用可能な構成とデータベースを使用して新しいクラスを構築する必要もありました。
以下のコード:
class yourclassname {
public function __construct($registry) {
$this->config = $registry->get('config');
$this->db = $registry->get('db');
}
}
$this->db->query("Your query here"); 経由でクエリを実行できます。