如何成为OpenCart大师? [关闭]

问题描述 投票:95回答:6

看起来他们没有文件,除了他们的官方论坛上的一些api电话。我有Zend框架和CodeIgniter框架的经验。任何OpenCart大师都可以向我推荐最好的学习方法,并在最短的时间内掌握它吗?我很快就要做一个大项目。

php opencart
6个回答
308
投票

OpenCart 1.5.X developer quick start guide for beginners

本指南是为熟悉PHP,OOP和MVC架构的开发人员编写的

在下文中,您将看到购物车目录端的示例。管理员方面的功能相同,但相关部分中注明的视图除外


了解图书馆

可以使用$this->library_name通过Controller,Model和Views访问所有库功能。所有这些都可以在/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来了解要加载的内容,并且是查找每个页面需要编辑的文件的基础功能。大多数路线实际上只使用aaa/bbb,它应该被视为两个部分,但是一些包含三个部分aaa/bbb/ccc第一部分aaa通常与通用文件夹中的文件夹相关,例如控制器或模板文件夹。第二部分通常与文件名有关,没有相关的.php.tpl扩展名。第三部分在下面的“理解控制器”一节中进行了解释


理解语言

语言存储在/catalog/language/子文件夹的your-language文件夹中。在此范围内,各个页面中使用的常规文本值存储在文件夹内的your-language.php文件中,因此对于目录端的英语语言,您将在catalog/language/english/english.php中找到值。对于特定页面文本,您需要页面的route(通常是这种情况,但并不总是因为您可以指定任何您喜欢的语言文件)。例如,搜索页面具有路径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 Case类名,称为Controller。这又是路由特有的,public后跟子文件夹名称和文件名,没有扩展大写。实际上并不需要大小写,但建议它易于阅读。值得注意的是,类名不会从子文件夹和文件名中获取除字母和数字之外的任何值。下划线被删除。

在课堂上是方法。声明private的类中的方法可以通过路径运行 - aaa/bbb不是。默认情况下,使用标准的两部分路径(上面的index()),调用默认的ccc方法。如果使用路线的第三部分(上面的account/return/insert),则将运行此方法。例如,/catalog/controller/account/return.php将加载insert文件和类,并尝试调用/catalog/model/方法


了解模型

OpenCart中的模型可以在$this->load->model('xxx/yyy'); 文件夹中找到,并根据功能而不是路由进行分组,因此您需要在控制器中加载它们。

xxx

这将加载名为yyy.php的子文件夹$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; 变量,该变量本质上是key => value对的数组。举个例子

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中找到,除非它不存在,在这种情况下将使用默认主题的模板。对于上面的搜索页面示例,该文件是aaa/bbb_ccc.tpl。对于有三个部分的路线,它通常在catalog/product_list.tpl,虽然没有硬性规则。在管理员中,大多数页面都遵循此规则,但列出项目的页面(如产品列表页面)位于catalog/product_form.tpl中,产品编辑表单位于$result = $this->db->query("SELECT * FROM `" . DB_PREFIX . "table`"); 中。同样,这些都没有设置,而是默认购物车的标准。

模板文件实际上只是另一个php文件,但是扩展名为.tpl并且实际上是在控制器文件中运行,因此您可以在控制器中编写的所有内容都可以在模板文件中运行(尽管不建议除非绝对必要)


了解数据库对象

使用查询运行

DB_PREFIX

$result顾名思义是一个包含数据库前缀的常量(如果存在)

SELECT将返回$result->row查询的对象,其中包含一些属性

如果一个或多个作为关联数组返回,则$result->rows包含第一行的数据

$result->num_rows包含一系列行结果,非常适合使用foreach进行循环

$this->db包含返回的结果数

$this->db->escape()对象还有一些额外的方法

mysql_real_escape_string()在传递的价值上使用$this->db->countAffected

UPDATE返回受$this->db->getLastId()查询影响的行数,依此类推

mysql_insert_id()使用$_GET返回最后一个自动增量ID


了解保留变量

OpenCart有预定义的变量来代替标准的$_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成为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
投票

全局库方法:基本的opencart库函数及其功能,其中大多数可以从目录中的任何位置或管理文件夹(控制器,模型,视图)中调用

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

9
投票

有一个OpenCart Wiki网站,上面有初学者开发人员的文档。请按照以下网址获取更多详细信息:

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

INTERNET ARCHIVE链接

http://web.archive.org/web/20160305131349/http://wiki.opencarthelp.com/doku.php?id=methods_reference over 5000 built-in functions

例如。方法参考有以下详细信息:

  1. 客户登录
  2. 数据库访问
  3. 购物车处理
  4. 配置
  5. 高速缓存
  6. 货币处理

仍然有一些页面正在建设中,但它会有所帮助。

[更新]

截至2018年1月,opencarhelp.com域名已经下降。


1
投票

PHP是array() count() explode() implode() mktime() delete() time() date() sprintf() list() 的一个相当大的语言,因此学习新平台的一个策略是确定它最常使用的功能,并花一些时间来了解它们。

我在OpenCart源代码上运行了一些查询,前10个最常用的函数是:

https://www.antropy.co.uk/blog/efficient-learning-for-new-opencart-developers/

这里列出的所有52个以及Linux bash命令都可以在任何代码库上用来识别常用函数:OpenCart Videos Tutorials


1
投票

这个youtube视频播放列表也有助于成为OpenCart开发人员Gurus:

Introduction and Table of Contents

  1. OpenCart installation localhost本视频介绍了该系列
  2. Files and folder structure of Opencart此视频通过localhost进行OpenCart安装
  3. Creating Database Table schema in OpenCart它描述了OpenCart的文件和文件夹结构
  4. OpenCart Library Predefined Objects’ Methods它显示了数据库表模式,并显示了如何在OpenCart中创建数据库表
  5. MVCL pattern, code flow and request & response in OpenCart它描述了OpenCart库预定义对象的方法,并显示了在哪里找到它们。
  6. MVCL described with Code它显示了OpenCart中的MVCL模式,代码流和请求与响应。他们描述了如下图所示的流程:Install, Configure and Uninstall Opencart module
  7. Layouts and position in Opencart 3它显示了三种上传模块的方法,然后安装,配置和卸载OpenCart 3模块/扩展。
  8. Event overview of Opencart它描述了OpenCart 3的布局和位置。它显示了如何显示不同页面的自定义布局,提供类别页面的示例。我们展示了不同类别的不同布局。
  9. Opencart API documentation for developer您将了解OpenCart中的事件,它们如何工作以及它们如何有用。
  10. index.php此视频将展示如何使用和制作自定义opencart API

一旦你看到这些视频,那么你可以开始编码:)


1
投票

虽然这个主题已经多次回答,但我想根据自己的经验提供另一种掌握OpenCart的方法。

Learning by doing

通过使用少量文件从头开始创建自己的OpenCart框架,您可以了解所有内容是如何组合在一起的。我将为您模仿OpenCart的文件结构。

创建一个文件<?php // My simpleCart

/system/engine/registry.php

1. Registry

Opencart使用Registry模式列出所有已加载类的实例。它是您的OpenCart应用程序的核心。然后,将注册表对象传递给每个类别,模型和库,以便快速访问其他对象。

使用路径<?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;

system/library/response.php

2.输出

现在让我们添加一个输出,将来我们的HTML。毕竟,整个想法是将一串文本发送到浏览器。

创建文件<?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作为示例。我们将进一步删除它。刷新您的网站以进行检查。浏览器应显示home

3.控制器

将控制器视为页面。他们将定义将向客户显示的内容:text,html,json,download甚至图像。现在,我们只想要一个发送文本的页面。

我们将为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

注意我如何将route传递给ControllerCommonHome,以便我可以在控制器内访问它。

4.路由器

我们不希望控制器被硬编码,对吧。我们将使用url地址中的参数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();
    }
}

创建Router类,负责根据路由初始化Controller文件(换句话说:动态调用控制器)

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,然后将其传递给https://www.youtube.com/dreamvention

这篇文章已经太长了,但我希望它能够对OpenCart中的MVC模式有一个基本的了解。

如果您希望我继续这篇文章,并告诉您其他事情如何像模型和视图一样工作,请对此答案进行评分,以便我知道。

另外看看我的Youtube https://dreamvention.com/blog和我的博客qazxswpoi我将在那里为你们发布更多的提示和教程!

© www.soinside.com 2019 - 2024. All rights reserved.