Bài 4: Tìm hiểu về kiến trúc trong Opencart

Reference from: http://code.tutsplus.com/articles/from-beginner-to-advanced-in-opencart-the-architecture–cms-21482

Up until this point, our previous articles focused on OpenCart’s user interface. In the articles, we discussed how to use the admin panel. Starting in this article, we’ll be understanding the OpenCart Framework which is based on MVC design pattern.

If you’re familiar with Model-View-Controller (MVC), then you’ve got a head start; otherwise, don’t worry about it we’ll discuss MVC architecture in our upcoming articles in thorough detail.

The OpenCart Framework is quite easy as it was designed to catch the attention of entry-level developers. OpenCart follows an easy URL Routes, like that:route=aa/bb. The “route” query string consists of at least two parts which gives instruction to framework what to load.

For example, in our above example I used aa as a first part and bb as second part. In the MVC architecture, it works like this:

First, the controller is loaded. In this case, the controller is identified by the first and second parameters in the query string. The first part of the query string identifies the directory, the second part of the query string identifies the filename of the controller (though without the relevant PHP extension).

So the above query string parameter relates to the file as given below:

(STORE ROOT DIRECTORY)/catalog/controller/aa/bb.php

Note: Route parameter can contain the third part i.e., route=aa/bb/cc. Third part will be discussed in future articles when we look at understanding controllers.

Libraries play a very important part in OpenCart. Essentially, libraries are helper files, which are most often used when developing something. OpenCart has given pre-built libraries that could be very helpful when writing code. The general syntax of loading a library like that follows this format: $this->library_name.  Libraries can be found in system/library.

For example, to programmatically log a user into the system, you can use $this->user->login($username,$password); and to log a user out of the system, you could use $this->user->logout();.

Some of the commonly used libraries:

  • cart.php which contain cart-related functions
  • image.php contains image-related functions such as like image cropping, caching, and more
  • customer.php includes customer-related functions
  • config.php is responsible for loading all of the OpenCart settings
  • user.php contains all of the administrator and user functions.

Languages can be located at catalog/language . In earlier articles, we added the French Language on our site, so inside the folder there would be theenglish and french folders. To more clearly understand this, we’ll take a look and what’s included in the english directory.

The Values that are used across many pages are stored is english.php. If you want to add a new value just follow the syntax below:

$_['language_key']='This is test';.

However, the languages based on special pages are located inside the folders. For example, for error page the language file can be found at: catalog/language/english/error/not_found.php .

Language file can be loaded into controller by using the syntax as below:

$this->language->load('error/not_found'); Then you can use the language library function “get” to fetch the language value:


Note: The values in the global language file english/english.php are automatically loaded and available to use without the $this->language->load method.

This article covered & explained the basic architecture of OpenCart System. But this is not all, There are a little more things to be explained in our next two articles.

To be a expert in some tool one must be more familiar with the System Architecture of the tool. So, in our next articles, we’ll be focusing on the core things. I hope you’ve enjoyed the article, till our next article please feel free to leave any comments and/or questions in the feed below.

2,240 total views, 1 views today

Trả lời

Next Post

Bài 5: Mô hình MVC trong Opencart

T5 Th8 6 , 2015
Reference from: http://code.tutsplus.com/tutorials/from-beginner-to-advanced-in-opencart-understanding-mvc–cms-21627 — In the previous tutorial, we discussed some of the basics for understanding the core OpenCart application. In this article, we are going to continue the discussion of the design pattern employed by OpenCart and even write our own controller. Before understanding the code architecture in OpenCart, We’ll first examine the Model-View-Controller (MVC) […]