From Beginner To Advanced in OpenCart: The Architecture

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 the english 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:

$this->language->get('language_key'); 

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.

文章來源

發佈留言