2018-07-25
MVC不是一個應用程序,基本上它遵循設計模式並基於分層架構。
MVC (Model View Controller)
Opencart 模型 視圖 控制器
是三層的,用於以明確定義的方式將數據相互傳遞。
Controller:在Opencart 的Controller扮演調解者的角色,管理整個程序控制。
當任何客戶通過瀏覽器點擊URL時,將調用控制器文件。
在控制器內部,我們可以加載模型並調用該模型文件的方法來獲取相關數據。從模型文件控制器獲得響應後,將數據發送到視圖文件。
我們還可以在Opencart的控制器中包含JS和CSS文件。
admin/coneroller/{..directory_name..}/{..file_name..}.php
catalog/coneroller/{..directory_name..}/{..file_name..}.php
Model:用於通過使用SQL查詢從數據庫獲取數據並將相同數據返回給控制器的模型文件。
模型文件主要用於通過SQL命令執行某些操作,如DDL(數據定義語言,即創建,修改,刪除,截斷等)和DML(數據操作語言,即選擇,插入,更新,刪除,合併等)。
admin/model/{..directory_name..}/{..file_name..}.php
catalog/model/{..directory_name..}/{..file_name..}.php
View:查看文件以php,json等格式從控制器文件接收結果數據,並將該數據查看到瀏覽器。
您可以在opencart中的視圖文件中編寫HTML,JS / JQuery,Css和PHP代碼。
我們對視圖文件使用.tpl擴展。
admin/view/template/{..directory_name..}/{..file_name..}.tpl
catalog/view/template/{..template_name,theme_name..}/{..directory_name..}/{..file_name..}.tpl
Language: 我們還使用一個名為language file的文件來進行與數組索引的sting映射。
Controller通過使用字符串轉換獲取所有索引並將這些數組索引呈現給.tpl文件來加載此語言文件。
admin/language/{..lanaguage_name..}/{..directory_name..}/{..file_name..}.php
catalog/language/{..lanaguage_name..}/{..directory_name..}/{..file_name..}.php
startup.php文件中,有一個名為modify($ filename)的方法,只有在使用ocmod.xml文件時才會返回修改文件的完整匹配路徑:
modification 這 function
// Modification Override
function modification($filename) {
if (defined('DIR_CATALOG')) {
$file = DIR_MODIFICATION . 'admin/' . substr($filename, strlen(DIR_APPLICATION));
} elseif (defined('DIR_OPENCART')) {
$file = DIR_MODIFICATION . 'install/' . substr($filename, strlen(DIR_APPLICATION));
} else {
$file = DIR_MODIFICATION . 'catalog/' . substr($filename, strlen(DIR_APPLICATION));
}
if (substr($filename, 0, strlen(DIR_SYSTEM)) == DIR_SYSTEM) {
$file = DIR_MODIFICATION . 'system/' . substr($filename, strlen(DIR_SYSTEM));
}
if (is_file($file)) {
return $file;
}
return $filename;
}
在system / startup.php文件中找到start(’catalog’),該文件將加載framework.php文件,其中創建所有使用的類的對象並將其設置為註冊表。
可以通過它訪問控制器中的所有系統類。
您將找到如下的類名: ( Controller/Sale/Invoice )
<?php
class ControllerSaleInvoice extends Controller {
private $error = array();
public function index() {
$this->load->language('sale/invoice');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('sale/invoice');
$this->getList();
}
在Case中,您應始終首先編寫Controller關鍵字而不是任何空格。
在任何控制器文件中,索引方法在通過URL命中控制器路由時首先調用。
例如調用 common/header
$data['header'] = $this->load->controller('common/header');
還是 common/footer
$data['footer'] = $this->load->controller('common/footer');
admin 左選單 … ‘common/column_left
$data['column_left'] = $this->load->controller('common/column_left');
加載腳本和样式表
在opencart中加載樣式和腳本文件有方法,通過控制器加載這些文件或在視圖文件(.tpl)上定義。
直接加載 …
$this->document->setTitle($this->language->get('heading_title'));
$this->document->setTitle($product_info['meta_title']);
$this->document->setDescription($product_info['meta_description']);
$this->document->setKeywords($product_info['meta_keyword']);
$this->document->setType('product');
$this->document->setImage($product_info['image']);
$this->document->setPrice($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
$this->document->addLink($this->url->link('product/product', 'product_id=' . $this->request->get['product_id']), 'canonical');
$this->document->addScript('catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js');
$this->document->addStyle('catalog/view/javascript/jquery/magnific/magnific-popup.css');
加載model
控制器上加載模型,並可以使用以下命令調用模型函數:
加載模型全部(sale/invoice) 或單一個 (catalog/information 內 InformationLayouts 的 information_id … public function getInformation($information_id )
$this->load->model('sale/invoice');
$data['information_layout'] = $this->model_catalog_information->getInformationLayouts($this->request->get['information_id']);
模板(視圖)文件
controller 發送 or 查看數據到view(.tpl)文件,使用以下代碼:
# use module controller , use as
return $this->load->view('common/header', $data);
# use layout controller , use as
$this->response->setOutput($this->load->view('catalog/product_list', $data));
controller 回應 json
$this->response->setOutput(json_encode($json));