web-dev-qa-db-ja.com

OpenCartの第一人者になるには?

公式フォーラムでのいくつかのAPI呼び出しを除いて、ドキュメントはないようです。私はZendフレームワークとCodeIgniterフレームワークの経験があります。 OpenCartのマスターは、最短時間で習得してマスターするための最良の方法を推奨できますか?私はすぐにそれで大きなプロジェクトをしなければなりません。

95
CodeCrack

OpenCart 1.5.X開発者向けクイックスタートガイド(初心者向け)

このガイドは、PHP、OOPおよびMVCアーキテクチャに既に精通している開発者向けに書かれています。

以下に、カートのカタログ側の例を示します。管理者側の機能は、関連するセクションに記載されているビューを除いて同じです


ライブラリについて

ライブラリ機能はすべて、$this->library_nameを使用して、コントローラー、モデル、およびビューからアクセスできます。これらはすべて/system/library/フォルダーにあります。たとえば、現在のショッピングカートの製品にアクセスするには、Cartクラスを使用する必要があります。このクラスは/system/library/cart.phpにあり、$this->cart->getProducts()を使用してアクセスできます

一般的に使用されるアイテム

  • customer.php-顧客関連の関数
  • user.php-管理者ユーザー関連の関数
  • cart.php-カート関連の関数
  • config.php-すべての設定はこれからロードされます
  • url.php-URL生成関数

ルートパラメータについて

OpenCartのフレームワークは、クエリ文字列パラメーターのroute=aaa/bbb/cccを使用してロードする内容を認識します。これは、各ページで編集する必要があるファイルを見つけるための基盤となる機能です。ほとんどのルートでは、実際には2つの部分として表示されるaaa/bbbのみを使用しますが、一部には3つの部分aaa/bbb/cccが含まれます。最初の部分aaa 2番目の部分は、通常、関連する.phpまたは.tpl拡張子のないファイル名に関連しています。 3番目の部分については、以下の「コントローラーについて」セクションで説明します


言語を理解する

言語は、/catalog/language/サブフォルダーのyour-languageフォルダーに保存されます。この中で、さまざまなページで使用される一般的なテキスト値はフォルダー内のyour-language.phpファイルに保存されるため、カタログ側の英語の場合は、catalog/language/english/english.phpに値があります。特定のページのテキストについては、ページのrouteが必要です(これは一般的なケースですが、alwaysは指定できません)任意の言語ファイル)。たとえば、検索ページにはproduct/searchというルートがあるため、そのページの言語固有のテキストはcatalog/language/english/product/search.phpにあります(ファイルの名前とサブフォルダーは、.phpが続くルートに一致します。

コントローラに言語をロードするには、次を使用します

$this->language->load('product/search');

次に、言語ライブラリ関数getを使用して、次のような特定の言語テキストを取得できます。

$some_variable = $this->language->get('heading_title');

言語変数は、キーとテキスト値の配列である特別な変数$_を使用して、言語ファイルに割り当てられます。 /catalog/language/english/product/search.phpに似たものが見つかるはずです

$_['heading_title']     = 'Search';

グローバル言語ファイルenglish/english.phpの値は自動的に読み込まれ、$this->language->loadメソッドなしで使用できます


コントローラーについて

コントローラはrouteに基づいてロードされ、理解するのはかなり簡単です。コントローラーは/catalog/controller/フォルダーにあります。最後の例から続けて、検索ページのコントローラーはこのフォルダー内の/product/search.phpにあります。再び.phpが続くルートが使用されることに注意してください。

コントローラーファイルを開くと、Controllerと呼ばれるControllerProductSearchクラスを拡張するPascalケースクラス名が表示されます。これもルートに固有であり、Controllerの後にサブフォルダー名とファイル名が続き、拡張子は大文字ではありません。大文字は実際には必要ありませんが、読みやすくするためにお勧めします。クラス名は、サブフォルダーとファイル名から文字と数字以外の値を取らないことに注意してください。アンダースコアは削除されます。

クラス内にはメソッドがあります。 publicと宣言されたクラスのメソッドは、ルートを介して実行するためにアクセス可能です-privateはアクセスできません。デフォルトでは、標準の2つの部分からなるルート(上記のaaa/bbb)で、デフォルトのindex()メソッドが呼び出されます。ルートの3番目の部分(上記のccc)が使用される場合、代わりにこのメソッドが実行されます。たとえば、account/return/insert/catalog/controller/account/return.phpファイルとクラスをロードし、insertメソッドを呼び出そうとします


モデルを理解する

OpenCartのモデルは/catalog/model/フォルダーにあり、ルートではなく機能に基づいてグループ化されています。したがって、コントローラーにモデルを読み込む必要があります。

$this->load->model('xxx/yyy');

これにより、yyy.phpというサブフォルダーxxxにファイルがロードされます。その後、オブジェクトを介して使用できます

$this->model_xxx_yyy

また、コントローラーと同様に、そのpublicメソッドのみを呼び出すことができます。たとえば、画像のサイズを変更するには、次のようにtool/imageモデルを使用し、そのresizeメソッドを呼び出します

$this->load->model('tool/image');
$this->model_tool_image->resize('image.png', 300, 200);

コントローラーからのビューでの変数の割り当てについて

コントローラからビューに値を渡すには、データを$this->data変数に割り当てる必要があります。これは、本質的にキー=>値のペアの配列です。例として

$this->data['example_var'] = 123;

ビューでこれにアクセスすることは、各キーを変数に変換する extract() メソッドに精通していれば、少し理解しやすいはずです。したがって、example_varキーは$example_varになり、ビューでそのようにアクセスできます。


テーマを理解する

テーマはカタログ側でのみ使用でき、基本的にはテンプレート、スタイルシート、テーマ画像のフォルダーです。テーマフォルダーは/catalog/view/theme/フォルダーに配置され、その後にテーマ名が続きます。 defaultフォルダーを除き、フォルダー名は重要ではありません

管理者側は/admin/view/template/を使用します(異なるテーマを許可しないため、パスから/theme/theme-name/をスキップします)

テンプレートファイルは、テーマフォルダー内のtemplateフォルダーにあります。現在選択されているテーマでテンプレートを使用できない場合は、代わりにデフォルトフォルダーのテンプレートがフォールバックとして使用されます。これは、非常に少ないファイルでテーマを作成でき、それでも完全に機能することを意味します。また、アップグレードが行われる際のコードの重複と問題を削減します


ビューについて(テンプレート)

言語やモデルと同様に、ビューファイルは一般的にルートに関連していますが、まったく同じである必要はありません。カタログ側のテンプレートは、存在しない場合は通常/catalog/view/theme/your-theme/template/にあります。存在しない場合は、デフォルトのテーマのテンプレートが使用されます。上記の検索ページの例では、ファイルはproduct/search.tplです。 3つの部分からなるルートの場合、一般的にはaaa/bbb_ccc.tplにありますが、ハードセットルールはありません。管理者では、ほとんどのページがこれに続きます。ただし、製品リストページなどのアイテムリストページはcatalog/product_list.tplにあり、製品編集フォームはcatalog/product_form.tplにあります。繰り返しますが、これらは設定されていませんが、デフォルトのカートの標準です。

テンプレートファイルは実際には別のphpファイルですが、.tpl拡張子が付いており、実際にコントローラーファイルで実行されるため、コントローラーでコーディングできるすべてのことをテンプレートファイルで実行できます(ただし、必要)


データベースオブジェクトについて

クエリは次を使用して実行されます

$result = $this->db->query("SELECT * FROM `" . DB_PREFIX . "table`");

DB_PREFIXは、名前が示すとおり、データベースプレフィックスが存在する場合はそれを含む定数です。

$resultは、いくつかのプロパティを含むSELECTクエリのオブジェクトを返します

1つ以上が連想配列として返される場合、$result->rowには最初の行のデータが含まれます

$result->rowsには行の結果の配列が含まれ、foreachを使用したループオーバーに最適です。

$result->num_rowsには、返された結果の数が含まれます

$this->dbオブジェクトが持っている追加のメソッドもいくつかあります

$this->db->escape()は、渡された値で mysql_real_escape_string() を使用します

$this->db->countAffectedは、UPDATEクエリなどの影響を受ける行の数を返します

$this->db->getLastId()は、 mysql_insert_id() を使用して最後の自動インクリメントIDを返します


予約変数について

OpenCartには、標準の$_GET$_POST$_SESSION$_COOKIE$_FILES$_REQUESTおよび$_SERVERの代わりに使用する定義済み変数があります。

$_SESSIONは、$this->session->dataを使用して編集されます。ここで、データは$_SESSIONを模倣した連想配列です。

他のすべては$this->requestを使用してアクセスでき、マジッククォートの有効化/無効化に準拠するように「クリーニング」されているため、

$_GET$this->request->getになります

$_POST$this->request->postになります

$_COOKIE$this->request->cookieになります

$_FILES$this->request->filesになります

$_REQUEST$this->request->requestになります

$_SERVER$this->request->serverになります


概要

上記は開発者向けの防弾ガイドではありませんが、開始するのに適した出発点となることを願っています

308
Jay Gilford

グローバルライブラリメソッド:基本的なopencartライブラリ関数とその機能。これらのほとんどは、カタログまたは管理フォルダー(コントローラー、モデル、ビュー)のどこからでも呼び出すことができます。

CACHE
$this->cache->delete($key) - Deletes cache [product, category, country, zone, language, currency,
manufacturer]

CART
$this->cart->getProducts() Gets all products currently in the cart including options, discounted prices, etc.
$this->cart->add( $product_id, $qty = 1, $options = array()) - Allows you to add a product to the cart
$this->cart->remove( $key ) - Allows you to remove a product from the cart
$this->cart->clear() - Allows you to remove all products from the cart
$this->cart->getWeight() - Sum of the weight of all products in the cart that have require shipping set to Yes
$this->cart->getSubTotal() - returns the subtotal of all products added together before tax
$this->cart->getTotal() - returns the total of all products added together after tax
$this->cart->countProducts() - returns the count of all product in the cart
$this->cart->hasProducts() - returns true if there is at least one item in the cart
$this->cart->hasStock() - returns false if there is at least one item in the cart that is out of stock
$this->cart->hasShipping() - returns true if there is at least one item in the cart that requires shipping
$this->cart->hasDownload() - returns true if there is at least one item in the cart that has a download
associated

CONFIG
$this->config->get($key) - returns setting value by keyname based on application (catalog or admin)
$this->config->set($key, $value) - set the value to override the setting value. DOES NOT SAVE TO DATABASE

CURRENCY
$this->currency->set($currency) - set or override the currency code to be used in the session
$this->currency->format($number, $currency = '', $value = '', $format = TRUE) - format the currency
$this->currency->convert($value, $from, $to) - convert a value from one currency to another. Currencies must
exist
$this->currency->getId() - get the database entry id for the current currency (1, 2, 3, 4)
$this->currency->getCode() - get the 3-letter iso code for the current currency (USD, EUR, GBP, AUD, etc)
$this->currency->getValue($currency) - get the current exchange rate from the database for the specified
currency.
$this->currency->has(currency) - Check if a currency exists in the opencart currency list

CUSTOMER
$this->customer->login($email, $password) - Log a customer in
$this->customer->logout() - Log a customer out
$this->customer->isLogged() - check if customer is logged in
$this->customer->getId() - get the database entry id for the current customer (integer)
$this->customer->getFirstName() - get customer first name
$this->customer->getLastName() - get customer last name
$this->customer->getEmail() - get customer email
$this->customer->getTelephone() - get customer telephone number
$this->customer->getFax() - get customer fax number
$this->customer->getNewsletter() - get customer newsletter status
$this->customer->getCustomerGroupId() - get customer group id
$this->customer->getAddressId() - get customer default address id (maps to the address database field)

DATABASE
$this->db->query($sql) - Execute the specified sql statement. Returns row data and rowcount.
$this->db->escape($value) - Escape/clean data before entering it into database
$this->db->countAffected($sql) - Returns count of affected rows from most recent query execution
$this->db->getLastId($sql) - Returns last auto-increment id from more recent query execution 4

DOCUMENT (*Called from controller only before renderer)
$this->document->setTitle($title) - Set page title
$this->document->getTitle()- Get page title
$this->document->setDescription($description) - Set meta description
$this->document->getDescription()- Get meta description
$this->document->setKeywords()- Set meta keywords
$this->document->getKeywords()- Get meta keywords
$this->document->setBase($base) - Set page base
$this->document->getBase() - Get page base
$this->document->setCharset($charset) - Set page charset
$this->document->getCharset() - Get page charset
$this->document->setLanguage($language) - Set page language
$this->document->getLanguage()- Get page language
$this->document->setDirection($direction) - Set page direction (rtl/ltr)
$this->document->getDirection()- Get page direction (rtl/ltr)
$this->document->addLink( $href, $rel ) – Add dynamic <link> tag
$this->document->getLinks()- Get page link tags
$this->document->addStyle( $href, $rel = 'stylesheet', $media = 'screen' ) – Add dynamic style
$this->document->getStyles()- Get page styles
$this->document->addScript( $script ) - Add dynamic script
$this->document->getScripts()- Get page scripts
$this->document->addBreadcrumb($text, $href, $separator = ' &gt; ') – Add breadcrumb
$this->document->getBreadcrumbs()- Get Breadcrumbs

ENCRYPT
$this->encryption->encrypt($value) - Encrypt data based on key in admin settings
$this->encryption->decrypt($value) - Decrypt data based on key in admin settings

IMAGE
$this->image->resize($width = 0, $height = 0)

JSON
$this->json->encode( $data )
$this->json->decode( $data , $assoc = FALSE)

LANGUAGE
$this->language->load($filename);

LENGTH
$this->length->convert($value, $from, $to) - convert a length to another. units must exist
$this->length->format($value, $unit, $decimal_point = '.', $thousand_point = ',') - format the length to use
unit

LOG
$this->log->write($message) - Writes to the system error log

REQUEST
$this->request->clean($data) - Cleans the data coming in to prevent XSS
$this->request->get['x'] - Same as $_GET['x']
$this->request->post['x'] - Same as $_POST['x']

RESPONSE
$this->response->addHeader($header) - additional php header tags can be defined here
$this->response->redirect($url) - redirects to the url specified

TAX
$this->tax->setZone($country_id, $zone_id) - Set the country and zone id for taxing (integer)
$this->tax->calculate($value, $tax_class_id, $calculate = TRUE) - Calculate all taxes to be added to the total
$this->tax->getRate($tax_class_id) - Get the rates of a tax class id
$this->tax->getDescription($tax_class_id) - Get the description of a tax class id
$this->tax->has($tax_class_id) - Check if a tax class id exists in opencart

SESSION
$this->session->data['x'] - Same as $_SESSION['x']  
36
Abdul Rehman

OpenCart Wiki Webサイトには、初心者開発者向けのドキュメントがあります。詳細については、以下のURLを参照してください。

http://wiki.opencarthelp.com/doku.php?id=start
http://wiki.opencarthelp.com/doku.php?id=methods_reference

インターネットアーカイブリンク

http://web.archive.org/web/20160305131349/http://wiki.opencarthelp.com/doku.php?id=starthttp://web.archive。 org/web/20160305131349/http://wiki.opencarthelp.com/doku.php?id = methods_reference

例えば。メソッドリファレンスには次の詳細があります。

  1. お客様ログイン
  2. DBアクセス
  3. ショッピングカートの取り扱い
  4. 構成
  5. キャッシュ
  6. 通貨処理

まだいくつかのページが作成中ですが、参考になるでしょう。

[更新]

2018年1月現在、opencarhelp.comドメインはダウンしています。

9
Dharmang

PHPは 5000以上の組み込み関数 を備えたかなり大きな言語であるため、新しいプラットフォームを学習するための戦略の1つは、最も頻繁に使用する関数を特定し、それらを非常によく知るために時間をかけることです。

OpenCartのソースコードに対していくつかのクエリを実行しましたが、最もよく使用される上位10の関数は次のとおりです。

array()
count()
explode()
implode()
mktime()
delete()
time()
date()
sprintf()
list()

ここにリストされている52個すべてと、よく使用される関数を識別するためにコードベースで使用できるLinux bashコマンド: https://www.antropy.co.uk/blog/efficient-learning-for-new-opencart-開発者/

1
Paul Feakins

このトピックはすでに何度も回答されていますが、私の経験に基づいてOpenCartをマスターするための別のアプローチを提供したいと思います。

実践的学習

少数のファイルを使用して独自のOpenCartフレームワークをゼロから作成することにより、すべてがどのように組み立てられるかを理解できます。 OpenCartのファイル構造を模倣します。

ファイルを作成するindex.php

<?php
// My simpleCart

1.レジストリ

Opencartは、レジストリパターンを使用して、ロードされたクラスのすべてのインスタンスをリストします。 OpenCartアプリの心臓部です。レジストリオブジェクトは、他のオブジェクトにすばやくアクセスするために、すべてのカテゴリ、モデル、およびライブラリに渡されます。

パス/system/engine/registry.phpでファイルを作成します

<?php
// Registry class. 
class Registry
{
    private $data = array();

    public function set($key, $value){
        $this->data[$key] = $value;
    }

    public function get($key){
        return (isset($this->data[$key])) ? $this->data[$key] : false;
    }
}

あなたのindex.php

<?php
// My simpleCart

//load dependency files
require_once('system/engine/registry.php');

//initialize registry
$registry = new Registry;

2.出力

次に、将来的にHTMLになる出力を追加しましょう。結局のところ、全体的なアイデアは、ブラウザーにテキスト文字列を送信することです。

ファイルを作成system/library/response.php

<?php
class Response {
    private $output;

    public function getOutput() {
        return $this->output;
    }

    public function setOutput($output) {
        $this->output = $output;
    }

    public function output() {
        if ($this->output) {
            echo $this->output;
        }
    }
}

そしてあなたのindex.php

<?php
// My simpleCart

//load dependency files
require_once('system/engine/registry.php');
require_once('system/library/response.php');

//initialize registry
$registry = new Registry;

//initialize response
$response = new Response;
//add response object to the registry
$registry->set('response', $response);

//lets set an output as a test
$registry->get('response')->setOutput('Hello World');

//send the output to the client
$registry->get('response')->output();

Hello worldを例としてのみ追加したことに注意してください。さらに削除します。サイトを更新して確認してください。ブラウザにHello Worldが表示されます。

3.コントローラー

コントローラをページと考えてください。クライアントに表示されるものを定義します:テキスト、html、json、ダウンロード、さらには画像。今のところ、テキストを送信するページが必要です。

homeページ用のコントローラーを作成します。

パスcatalog/controller/common/home.phpを持つファイルを追加します

<?php

class ControllerCommonHome{

    private $registry = array();

    public function __construct($registry){
        $this->registry = $registry;
    }

    public function index(){

        $output = 'Home Page';
        //using the registry to get the response object and set the Output
        $this->registry->get('response')->setOutput($output);
    }
}

index.phpを編集します

<?php
// My simpleCart

//load registry
require_once('system/engine/registry.php');
//load response
require_once('system/library/response.php');

//initialize registry
$registry = new Registry;

//initialize response
$response = new Response;
//add resoinse object to the registry
$registry->set('response', $response);

//load controller common/home
require_once('catalog/controller/common/home.php');
$controller = new ControllerCommonHome($registry);
$controller->index();

//send the output to the client
$registry->get('response')->output();

$refistryをControllerCommonHomeに渡して、コントローラー内でアクセスできるようにしたことに注目してください。

4.ルーター

コントローラーをハードコードすることは望ましくありません。 urlアドレスのパラメーターrouteを使用して、ロードするコントローラーをカートに伝えます。

パスsystem/library/request.phpでファイルを作成します

<?php
class Request {
    public $get = array();

    //for now I just need the $_GET parameter
    public function __construct() {
        $this->get = $_GET;
    }
}

ルートに基づいてコントローラーファイルを初期化するルータークラスを作成します(つまり、コントローラーを動的に呼び出します)。

<?php
class Router {
    private $registry;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function dispatch($route) {
        require_once('catalog/controller/'.$route.'.php');
        $class = "Controller".str_replace('/', '', $route);
        $controller = new $class($this->registry);
        $controller->index();
    }
}

index.phpにロードします

<?php
require_once('system/engine/registry.php');
require_once('system/engine/router.php');
require_once('system/library/response.php');
require_once('system/library/request.php');

$registry = new Registry;

$response = new Response;
$registry->set('response', $response);

$request = new Request;
$registry->set('request', $request);

//get the route from the url
if(isset($registry->get('request')->get['route'])){
    $route = $registry->get('request')->get['route'];
}else{
    $route = 'common/home';
}

//initiate the router and dispatch it base on the route
$router = new Router($registry);
$router->dispatch($route);


$registry->get('response')->output();

すべてを$registryにロードしてから$routerに渡し、$controllerに渡すことに注意してください。

この投稿はすでに長すぎますが、OpenCartのMVCパターンの基本的な理解が得られることを願っています。

この投稿を続けて、モデルやビューなど他の機能がどのように機能するかを教えてほしい場合は、この回答を評価してください。

また、Youtube https://www.youtube.com/dreamvention とブログ https://dreamvention.com/blog をチェックしてください。ヒントとチュートリアルを投稿します皆さんのために!

1
Dmitriy Zhuk

このYouTubeビデオプレイリストは、OpenCart開発者Gurusになるのにも役立ちます。

OpenCartビデオチュートリアル

  1. 紹介と目次 このビデオはシリーズの紹介を通過します
  2. OpenCart installation localhost このビデオは、localhostのOpenCartインストールを通過します
  3. Opencartのファイルとフォルダー構造 OpenCartのファイルとフォルダー構造について説明します
  4. OpenCartでのデータベーステーブルスキーマの作成 データベーステーブルスキーマを示し、OpenCartでデータベーステーブルを作成する方法を示します
  5. OpenCartライブラリ事前定義オブジェクトのメソッド OpenCartライブラリ事前定義オブジェクトのメソッドについて説明し、それらの場所を示しています。
  6. OpenCartのMVCLパターン、コードフロー、要求と応答 OpenCartのMVCLパターン、コードフロー、要求と応答を示します。以下の図のように、フローを説明します。 MVCL described with Code

  7. Opencartモジュールのインストール、設定、アンインストール モジュールをアップロードし、OpenCart 3モジュール/拡張機能をインストール、設定、アンインストールする3つの方法を示しています。

  8. Opencart 3のレイアウトと位置 OpenCart 3のレイアウトと位置について説明しています。さまざまなページのレイアウトをカスタマイズして表示する方法を示し、カテゴリページの例を示します。異なるカテゴリの異なるレイアウトを示します。

  9. Opencartのイベントの概要 OpenCartのイベントとは何か、どのように機能するのか、何がとても役立つのかを学びます。

  10. 開発者向けOpencart APIドキュメント このビデオでは、カスタムopencart APIの使用方法と作成方法を示します

これらのビデオを見たら、コーディングを開始できます:)

1
Rupak Nepali