看起来他们没有文件,除了他们的官方论坛上的一些api电话.我有Zend框架和CodeIgniter框架的经验.任何OpenCart大师都可以向我推荐最好的学习方法,并在最短的时间内掌握它吗?我很快就要做一个大项目.
Jay*_*ord 309
本指南是为熟悉PHP,OOP和MVC架构的开发人员编写的
在下文中,您将看到购物车目录端的示例.管理员方面的功能相同,但相关部分中注明的视图除外
所有库功能都可以通过Controller,Model和Views使用$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
查询字符串参数来了解要加载的内容,并且是查找每个页面需要编辑的文件的基础功能.大多数路由实际上只使用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');
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用语言库函数get
来检索特定的语言文本,例如
$some_variable = $this->language->get('heading_title');
Run Code Online (Sandbox Code Playgroud)
使用特殊变量在语言文件中分配语言变量,该变量$_
是键和文本值的数组.在你的/catalog/language/english/product/search.php
,你应该找到类似的东西
$_['heading_title'] = 'Search';
Run Code Online (Sandbox Code Playgroud)
全局语言文件english/english.php
中的值将自动加载,并且无需$this->language->load
方法即可使用
控制器基于此加载route
并且非常直接理解.控制器位于/catalog/controller/
文件夹中.继续上一个示例,"搜索"页面的控制器/product/search.php
位于此文件夹中.再次注意,使用了后面的路由.php
.
打开控制器文件,您将看到一个扩展Controller
该类的Pascal Case类名,称为ControllerProductSearch
.这又是路由特有的,Controller
后面跟着没有扩展大写的子文件夹名称和文件名.实际上并不需要大小写,但建议它易于阅读.值得注意的是,类名不会从子文件夹和文件名中获取除字母和数字之外的任何值.下划线被删除.
在课堂上是方法.声明的类中的方法public
可以通过路径运行 - private
不是.默认情况下,使用标准的两部分路径(aaa/bbb
上图),将index()
调用默认方法.如果使用路径的第三部分(ccc
上面),则将运行此方法.例如,account/return/insert
将加载/catalog/controller/account/return.php
文件和类,并尝试调用该insert
方法
OpenCart中的模型可以在/catalog/model/
文件夹中找到,并根据功能而不是路由进行分组,因此您需要在控制器中加载它们.
$this->load->model('xxx/yyy');
Run Code Online (Sandbox Code Playgroud)
这将在被xxx
调用的子文件夹中加载文件yyy.php
.然后可以通过对象使用它
$this->model_xxx_yyy
Run Code Online (Sandbox Code Playgroud)
和控制器一样,你只能调用它的public
方法.例如,要调整图像大小,您可以使用该tool/image
模型并按resize
如下方式调用其方法
$this->load->model('tool/image');
$this->model_tool_image->resize('image.png', 300, 200);
Run Code Online (Sandbox Code Playgroud)
为了从控制器向视图传递值,您只需将数据分配给$this->data
变量,该变量本质上是一个key => value对的数组.举个例子
$this->data['example_var'] = 123;
Run Code Online (Sandbox Code Playgroud)
如果您熟悉将每个键转换为变量的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
.同样,这些都没有设置,而是默认购物车的标准.
模板文件实际上只是另一个php文件,但是扩展名为.tpl并且实际上是在控制器文件中运行,因此您可以在控制器中编写的所有内容都可以在模板文件中运行(尽管不建议除非绝对必要)
使用查询运行
$result = $this->db->query("SELECT * FROM `" . DB_PREFIX . "table`");
Run Code Online (Sandbox Code Playgroud)
DB_PREFIX
顾名思义是一个包含数据库前缀的常量(如果存在)
$result
将返回一个SELECT
查询对象,其中包含一些属性
$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
虽然以上内容对开发人员来说不是一个防弹指南,但希望它可以作为入门者的良好起点
小智 36
全局库方法:基本的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 = ' > ') – 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']
Run Code Online (Sandbox Code Playgroud)
有一个OpenCart Wiki网站,提供初学者开发人员的文档.请按照以下网址获取更多详细信息:
http://wiki.opencarthelp.com/doku.php?id=start
http://wiki.opencarthelp.com/doku.php?id=methods_reference
例如方法参考有以下详细信息:
仍然有一些页面正在建设中,但它会有所帮助.
[更新]
截至2018年1月,opencarhelp.com域名已经下降.
虽然这个话题已经被回答过很多次了,但我想根据我的经验提供另一种掌握 OpenCart 的方法。
通过使用少量文件从头开始创建您自己的 OpenCart 框架,您可以了解所有内容是如何组合在一起的。我将为您模仿 OpenCart 的文件结构。
创建文件index.php
<?php
// My simpleCart
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在你的index.php
<?php
// My simpleCart
//load dependency files
require_once('system/engine/registry.php');
//initialize registry
$registry = new Registry;
Run Code Online (Sandbox Code Playgroud)
现在让我们添加一个输出,它将成为我们将来的 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在你的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();
Run Code Online (Sandbox Code Playgroud)
请注意,我添加 Hello world 仅作为示例。我们将进一步删除它。刷新您的网站进行检查。浏览器应该显示
Hello World
.
将控制器视为页面。他们将定义向客户端显示的内容:文本、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);
}
}
Run Code Online (Sandbox Code Playgroud)
并编辑你的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();
Run Code Online (Sandbox Code Playgroud)
请注意我如何将 传递
$refistry
给 ControllerCommonHome,以便我可以在控制器内访问它。
我们不希望控制器被硬编码,对吧。route
我们将使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
创建 Router 类,该类将负责根据路由初始化 Controller 文件(换句话说:动态调用控制器)
<?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();
}
}
Run Code Online (Sandbox Code Playgroud)
将其加载到您的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();
Run Code Online (Sandbox Code Playgroud)
请注意我如何将所有内容加载到 中
$registry
,然后将其传递给$router
,然后将其传递给$controller
。
这篇文章已经太长了,但我希望它能让您对 OpenCart 中的 MVC 模式有一个基本的了解。
如果您希望我继续这篇文章并告诉您其他事物(例如模型和视图)如何工作,请评价这个答案,以便我知道。
另请查看我的 Youtube https://www.youtube.com/dreamvention和我的博客https://dreamvention.com/blog我将在那里为你们发布更多提示和教程!
归档时间: |
|
查看次数: |
64572 次 |
最近记录: |