从零开始OpenCart3:Understanding MVC 了解MVC(4)

This post is part of a series called From Beginner To Advanced in OpenCart.

From Beginner To Advanced in OpenCart: The Architecture

From Beginner to Advanced in OpenCart: Module Development

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) Architecture.

What is MVC?

In modern web programming, the MVC architecture is widely used. Many of the most popular frameworks employ MVC; however, it is not an application, it is a design pattern - a prescribed solution to a common problem organized in code. 

Similarly, It is also known as an architectural pattern. MVC can be considered as layer based architecture. That is, the controller, model, and view are three layers all of which are related to one another.

1. Controllers

Controllers hold the main control of the program. A controller is responsible for the program flow. In web-based MVC applications, it is also the first layer which is called when the browser hits the URL. 

2. Models

Models hold the main data of the program such as information from database objects and SQL queries. All data is gained from the models; however, the models cannot be directly called - it is a controller who requests a model for specific data and the model performs the request and returns the data to that controller.

3. Views

A view is the last layer of the MVC Architecture which holds the user interface of the program. In web applications, a view contains HTML, CSS, JavaScript, XML or JSON, etc. The view is a visible layer to the user whereas the models and controllers are hidden from the user.

Let's understand that with the help of conversation form when a user hits the URL:

  • Controller: Hey ABC Model: The user wants the XYZ Data, please pass it to me.
  • 控制器:嘿 ABC 模块:用户需要 XYZ 的数据,请传送给我。
  • Model: Got the XYZ Data! Here it is.
  • 模型:收到 XYZ 的数据了!在这。
  • Controller: Hey ABC View, I am passing the XYZ Data to you.
  • 控制器:嘿 ABC 视图,我正在传送 XYZ 的数据给你。
  • View: Renders the information on the page for the user to see.
  • 视图:给用户在页面上呈现这条信息。

Understanding MVC in OpenCart

1. Controllers

As discussed above, A controller is the first layer of MVC Architecture. So let's take a look at this within the context of our site. We are going to create a new controller to help us gain a better understanding.

  1. Create a empty test directory in controllers directory i.e., catalog/controller/test
  2. Create a abc.php PHP File inside the test directory i.e., catalog/controller/test/abc.php.
  3. Edit the abc.php in your IDE and add the following code:

1

2

3

4

5

6

7

8

9

<?php

class ControllerTestAbc extends Controller{

     public function index()

     {

          echo 'Hello World';

          exit;

     }

}

?>

In above example, there are some OpenCart Framework constraints that should be strictly followed by the programmer.

  1. For a controller class name, It should be Like Controller(FolderName)(Filename)  . Things to Remember: The first case of folder name and filename must be in uppercase, and don't include file extension as a class name.     控制器类名命名规则为 < 控制器(文件夹名)(文件名)>。类名首字母必须大写,但是不要添加文件扩展名。
  2. The index() is the main function that is executed automatically on controller call
  3. index()  function must be public.
  4. Don't use HTML, JavaScript, or CSS in the controller 

Now go to your browser and type the URL: http://localhost/Opencart/index.php?route=test/abc

Summary

Bingo! You've made your first controller. Isn't it easy? This is fair enough for this tutorial as we created our first controller! A controller is the first and most important layer of MVC because MVC can exist without the model and view but not without the controller.

控制器是首要的且最重要的MVC的一个层,因为MVC模式可以仅有控制器而没有模型和视图存在。

In our next article, we'll be creating models and views as well as taking a look the route parameter with respect to controller. I hope that you've enjoyed this article. Happy Coding!

在下一章,我们将建立模型和视图,看一看带有路由参数的控制器回应。我希望你会享受这个章节。祝编程愉快!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值