2014-08-13

Previously, we reviewed the Model-View-Controller - or MVC - architecture of OpenCart. In this regard, we created our first "Hello World!" Controller. So, In the last of the previous article, i just discussed some basic things of Controller.

I hope you've practiced creating Controller as we're now taking ahead the discussion of controllers to the advanced level, but before starting the discussion let's get an overview what we're going to discuss in this article.

Writing a real world controller

Loading languages

Loading and using libraries

Libraries cheat sheet

Creating a view

Creating a model

Model manipulation with a controller

Conclusion

Writing a Real World Controller

We're going to create a simple user form following the OpenCart MVC Framework. For this, we need to create a controller first. As I've created a directory forms inside the catalog/controller

Note: Mac and Linux Users, don't forget to change your directory permission to 775.

Inside the forms directory, I've created a file myform.php . Now let's get started writing the code.

Review the code again and over again and try to create a controller like this.

Loading the Languages

OpenCart gives an easy manipulation & playing with languages. So, in the above example, the hard-coded language texts were used. But it is recommended to use the text manipulation.

To load a language, use $this->language->load(route path of the language); & to get the specific language data use this syntax $this->language->get(language file data keyword); .

Note that it is highly recommended to make a language file of each controller and page. The Language route path should also be same as controller.

Loading and Using Libraries

There are a couple of precoded libraries available for the ease of the developer. Libraries can be located in /system/library . Libraries can be directly loaded into Controller by using the syntax: $this->library_name->method_name().

Previously, We've studied a few number of libraries, so you can manipulate them with your code. Some cheat sheet is available as following:

Cart Library

Config Library

Customer Library

Database Library

Document Library

Creating a View

In OpenCart, there are always templates what are used for views for displaying the output. In the above controller we've specified the view file location to theme/default/template/forms/myform.tpl.

Now, create a file with that name and place the code below:

Creating a Model

A model holds the interaction with the database and its objects. So, before working on model, go to phpMyAdmin, select your store database (i.e., storedb) and execute this in SQL Panel

"oc" is a table prefix, use your own Store DB Prefix.

Now, it is time to create your own Model. The purpose behind creating a Model to save the form data into the Database. so lets create our first model.

Create a new file myforms.php in catalog/model/forms/and place the code as below:

Model Manipulation with Controller

So, our final controller code will look like this:

So, the condition will check if there is any data coming from POST method, if yes, then the database entry will be occured.

Note that you can add your validations and checks to validate the POST data.

Conclusion

Bingo! We have coded a real world Model-View-Controller application into OpenCart. You can explore more things and dive into the MVC sea; however, this was a basic introduction to MVC pattern and we've achieved our milestone.

In our future articles, we'll be discussing the modules and libraries creation of OpenCart. Please do provide your previous feedback on article in the comment feed below.

Show more