如何成为OpenCart专家? [英] How to become an OpenCart guru?
问题描述
除了官方论坛上的一些api调用外,他们似乎没有任何文档.我有Zend框架和CodeIgniter框架的经验. OpenCart的任何高手都可以向我推荐学习它的最佳方法,并在最短的时间内对其进行掌握吗?我必须尽快做一个大项目.
It seems like they have no documentation except some api calls on their official forums. I have experience with Zend framework and CodeIgniter framework. Can any OpenCart masters recommend me the best way to learn it and master in shortest amount of time? I have to do a big project with it soon.
推荐答案
OpenCart 1.5.X开发人员针对初学者的快速入门指南
本指南是为已经熟悉PHP,OOP和MVC体系结构的开发人员编写的
OpenCart 1.5.X developer quick start guide for beginners
This guide is written for developers already familiar with PHP, OOP and the MVC architecture
在下面,您将看到购物车目录侧的示例.管理员方面的功能是相同的,除了相关部分中提到的视图之外
In the following, you'll see examples for the catalog side of the cart. The admin side is identical in function with the exception of the views which is noted in the relevant section
所有库功能都可以使用$this->library_name
通过Controller,Model和Views访问.所有这些都可以在/system/library/
文件夹中找到.例如,要访问当前购物车的产品,您需要使用Cart
类,该类位于/system/library/cart.php
中,可以使用$this->cart->getProducts()
All of the library functionality is accessible through Controller, Model and Views using $this->library_name
. All of these can be found in the /system/library/
folder. For example, to access the current shopping cart's products, you'll need to use the Cart
class, which is in /system/library/cart.php
and can be accessed using $this->cart->getProducts()
常用物品
-
customer.php
-与客户相关的功能 -
user.php
-管理员用户相关功能 -
cart.php
-购物车相关功能 -
config.php
-所有设置均从此加载 -
url.php
-URL生成功能
customer.php
- Customer related functionsuser.php
- Admin user related functionscart.php
- Cart related functionsconfig.php
- All settings are loaded from thisurl.php
- URL generation functions
OpenCart的框架依靠查询字符串参数中的route=aaa/bbb/ccc
知道要加载的内容,并且是查找需要为每个页面编辑的文件的基础功能.实际上,大多数路由只使用aaa/bbb
,这应该看作两个部分,但是有些包含三个部分.aaa/bbb/ccc
第一部分aaa
通常与通用文件夹(例如,控制器或模板文件夹)中的文件夹有关.第二部分通常与文件名有关,没有相关的.php
或.tpl
扩展名.第三部分在下面的了解控制器"部分中进行了解释
OpenCart's framework relies on the route=aaa/bbb/ccc
in the query string parameter to know what to load, and is the underpinning feature to finding the files you need to edit for each page. Most route's actually only use the aaa/bbb
which should be seen as two parts, however some contain three parts aaa/bbb/ccc
The first part aaa
generally related to the folder within a generic folder such as the controller or template folders. The second part usually relates to the file name, without the relevant .php
or .tpl
extension. The third part is explained in the section "Understanding controllers" below
语言存储在your-language
子文件夹的/catalog/language/
文件夹中.其中,跨页面使用的常规文本值存储在文件夹内的your-language.php
文件中,因此对于目录侧的英语,您可以在catalog/language/english/english.php
中找到这些值.对于特定的页面文本,您需要页面的route
(通常是这种情况,但并非总是 ,因为您可以指定所需的任何语言文件).例如,搜索页面的路由为product/search
,因此该页面的语言特定文本可以在catalog/language/english/product/search.php
中找到(请注意,文件名和子文件夹与.php
后面的路由匹配.
Languages are stored in /catalog/language/
folder in the your-language
subfolder. Within this, general text values used across various pages are stored in the your-language.php
file inside the folder, so for the English language on the catalog side, you'll find the values in catalog/language/english/english.php
. For specific page text, you'll need the route
for the page (This is generally the case, but not always as you can specify any language file you like). For example, the search page has the route product/search
, and therefore the language specific text for that page can be found in catalog/language/english/product/search.php
(Notice the file's name and subfolder match the route followed by .php
.
要在控制器中加载语言,请使用
To load the language in a controller, you use
$this->language->load('product/search');
然后,您可以使用语言库功能get
检索特定的语言文本,例如
Then you can use the language library function get
to retrieve specific language texts, such as
$some_variable = $this->language->get('heading_title');
语言变量是使用特殊变量$_
在语言文件中分配的,特殊变量$_
是键和文本值的数组.在您的/catalog/language/english/product/search.php
中,您应该找到与
The language variables are assigned in the language file using a special variable $_
which is an array of keys and text values. In your /catalog/language/english/product/search.php
you should find something similar to
$_['heading_title'] = 'Search';
全局语言文件english/english.php
中的值会自动加载,无需使用$this->language->load
方法即可使用
The values in the global language file english/english.php
are automatically loaded and available to use without the $this->language->load
method
控制器是基于route
加载的,很容易理解.控制器位于/catalog/controller/
文件夹中.从上一个示例继续,搜索控制器"页面位于此文件夹中的/product/search.php
中.再次注意,使用了后跟.php
的路由.
Controllers are loaded based on the route
and are fairly straight forward to understand. Controllers are located in the /catalog/controller/
folder. Continuing from the last example, the Controller for the Search page is in /product/search.php
within this folder. Notice again that the route followed by .php
is used.
打开控制器文件,您将看到Pascal Case类名扩展了名为ControllerProductSearch
的Controller
类.这又是特定于路由的,在Controller
之后是子文件夹名称和文件名,而没有大写的扩展名.实际不需要大写,但是为了易于阅读,建议使用大写.值得注意的是,类名除了字母和数字外,不从子文件夹和文件名中获取任何值.下划线将被删除.
Opening the controller file, you'll see a Pascal Case classname extending the Controller
class, called ControllerProductSearch
. This again is specific to the route, with Controller
followed by the subfolder name and file name without the extension capitalised. The capitalisation is not actually required, but it's recommended for easy readability. It's worth noting that classnames don't take any values from the subfolder and file name other than letters and numbers. Underscores are removed.
在类中是方法.声明为public
的类中的方法可通过路由访问,而-private
则不可.默认情况下,使用标准的两部分路由(上面的aaa/bbb
),将调用默认的index()
方法.如果使用路由的第三部分(上面的ccc
),则将改为运行此方法.例如,account/return/insert
将加载/catalog/controller/account/return.php
文件和类,并尝试调用insert
方法
Within the class are the methods. Methods in the class declared public
are accessible to be run via the route - private
are not. By default, with a standard two part route (aaa/bbb
above), a default index()
method is called. If the third part of a route (ccc
above) is used, this method will be run instead. For example, account/return/insert
will load the /catalog/controller/account/return.php
file and class, and try to call the insert
method
OpenCart中的模型位于/catalog/model/
文件夹中,并且根据功能(而非路由)进行分组,因此您需要通过
Models in OpenCart are found in the /catalog/model/
folder and are grouped based on function, not route, and therefore you will need to load them in your controller via
$this->load->model('xxx/yyy');
这会将文件加载到名为yyy.php
的子文件夹xxx
中.然后可以通过对象使用它
This will load the file in the subfolder xxx
called yyy.php
. It is then available to use via the object
$this->model_xxx_yyy
,与控制器一样,您只能调用其public
方法.例如,要调整图像大小,可以使用tool/image
模型并按如下所示调用其resize
方法
and as with controllers, you can only call its public
methods. For instance, to resize an image, you would use the tool/image
model and call its resize
method as follows
$this->load->model('tool/image');
$this->model_tool_image->resize('image.png', 300, 200);
了解控制器视图中的变量分配
为了将值从控制器传递到视图,您只需要将数据分配给$this->data
变量,该变量实际上是键=>值对的数组.例如
Understanding variable assignment in views from the controller
In order to pass values to the view from the controller, you simply need to assign your data to the $this->data
variable, which is essentially an array of key => value pairs. As an example
$this->data['example_var'] = 123;
如果您熟悉 extract()将每个键转换为变量的方法.因此,example_var
键变为$example_var
,可以在视图中这样访问.
Accessing this in a view is a little should be easy to understand if you're familiar with the extract() method which converts each key into a variable. So the example_var
key becomes $example_var
and can be accessed as such in the view.
主题仅在目录侧可用,并且基本上是模板,样式表和主题图像的文件夹.主题文件夹放置在/catalog/view/theme/
文件夹中,后跟主题名称.除了default
文件夹
Themes are available to the catalog side only, and are basically a folder of templates, stylesheets and theme images. Theme folders are placed in the /catalog/view/theme/
folder followed by the theme name. The folder name isn't of importance with exception to the default
folder
管理员端使用/admin/view/template/
(由于路径中不允许使用不同的主题,因此将/theme/theme-name/
跳过了路径)
The admin side uses /admin/view/template/
(skipping the /theme/theme-name/
from the path as it doesn't allow differing themes)
模板文件位于主题文件夹中的template
文件夹中.如果没有任何模板可用于当前选定的主题,则使用默认文件夹的模板作为后备.这意味着可以用很少的文件创建主题,并且仍然可以正常使用.它还可以减少代码重复和升级过程中的问题
Template files reside in a template
folder within the theme folder. Should any template not be available for the currently selected theme, the default folder's template is used instead as a fallback. This means themes can be created with very few files and still function fully. It also reduces code duplication and issues as upgrades are made
与语言和模型一样,视图文件通常与路径相关,尽管完全没有必要.除非不存在,否则通常在/catalog/view/theme/your-theme/template/
中找到目录侧的模板,在这种情况下,将使用默认主题的模板.对于上面的搜索页面示例,文件为product/search.tpl
.对于三部分的路线,尽管没有硬性规定,但通常在aaa/bbb_ccc.tpl
中.在管理员中,大多数页面都遵循此规则,除了页面列表项(如产品列表页)位于catalog/product_list.tpl
中和产品编辑表单位于catalog/product_form.tpl
中之外.再说一次,这些没有设置,但是是默认购物车的标准.
As with language and models, the view file's are generally related to the route, though don't have to be at all. Templates on the catalog side are usually found in /catalog/view/theme/your-theme/template/
unless it doesn't exist, in which case the default theme's templates will be used. For our search page example above, the file is product/search.tpl
. For routes with three parts, it is generally in aaa/bbb_ccc.tpl
though there's no hard set rule. In the admin, most pages follow this, with the exception that pages listing items, like the product listing page, are in catalog/product_list.tpl
and the product editing form is in catalog/product_form.tpl
. Again, these aren't set, but a standard for the default cart.
模板文件实际上只是另一个php文件,但具有.tpl扩展名,并且实际上在控制器文件中运行,因此,您可以在控制器中进行编码的所有内容都可以在模板文件中运行(尽管不是除非绝对必要,否则建议使用)
The template file is in fact just another php file, but with a .tpl extension and is actually run in the controller file, therefore all of the things you can code in a controller can be run in a template file (though not recommended unless absolutely necessary)
使用以下命令运行查询
$result = $this->db->query("SELECT * FROM `" . DB_PREFIX . "table`");
顾名思义,
DB_PREFIX
是一个包含数据库前缀(如果存在的话)的常量
DB_PREFIX
as the name suggests is a constant containing the database prefix if one exists
$result
将为SELECT
查询返回一个对象,其中包含一些属性
$result
will return an object for SELECT
queries, containing a few properties
$result->row
包含第一行的数据(如果一个或多个作为关联数组返回)
$result->row
contains the first row's data if one or more are returned as an associative array
$result->rows
包含一个行结果数组,非常适合使用foreach进行循环
$result->rows
contains an array of row results, ideal for looping over using foreach
$result->num_rows
包含返回的结果数
$this->db
对象还具有一些其他方法
There are also a few extra methods the $this->db
object has
$this->db->escape()
在传递的值上使用 mysql_real_escape_string()
$this->db->countAffected
返回受UPDATE
查询影响的行数,依此类推
$this->db->countAffected
returns the number of rows affected by an UPDATE
query and so on
$this->db->getLastId()
使用 mysql_insert_id()
OpenCart具有预定义的变量以代替标准$_GET
,$_POST
,$_SESSION
,$_COOKIE
,$_FILES
,$_REQUEST
和$_SERVER
OpenCart has predefined variables to use in place of the standard $_GET
, $_POST
, $_SESSION
, $_COOKIE
, $_FILES
, $_REQUEST
AND $_SERVER
$_SESSION
,其中数据是模仿$_SESSION
可以使用$this->request
来访问所有其他内容,并且已被清除"以符合启用/禁用的魔术引号,因此
All of the others can be accessed using $this->request
and have been "cleaned" to comply with magic quotes enabled/disabled, so
$_GET
变为$this->request->get
$_POST
变为$this->request->post
$_COOKIE
变为$this->request->cookie
$_FILES
变为$this->request->files
$_REQUEST
变为$this->request->request
$_SERVER
变为$this->request->server
尽管以上内容并不是针对开发人员的防弹指南,但希望它可以为那些入门的人提供一个良好的起点
While the above isn't a bulletproof guide for developers, hopefully it will serve as a good starting point for those getting started
这篇关于如何成为OpenCart专家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!