


Examples to explain the meaning and division of responsibilities of MVC architecture
I was recently responsible for a project that used the MVC framework of Yii Framework. At first I thought the structure was very robust.
But as we deepened our understanding of business logic, we began to realize the seriousness of the problem.
I misunderstood Controller in MVC, and based on past experience, I took it for granted that all business logic was placed in the action# of Controller ## to implement.
Controller has thousands of lines of code, which is becoming more and more bloated.
Finally, I made up my mind to reconstruct the code. The origin was the need for an open API interface. According to the current architecture, the code is basically impossible to reuse. I need to write many functions over and over again, which is really unacceptable. Object-oriented programming is not just a term in textbooks! Only when I started to practice did I realize how rare it is to have object-oriented awareness and a global perspective.1 What exactly is MVC
Model-View-Controller (MVC) is a type ofDesign framework (design pattern) .
Thegoal of MVC is to separate business logic from user interface considerations.
This way, developers can more easily change each part without affecting the others. In MVC,- Model represents
- data and business rules; View contains
- User interface elements, such as text, forms, etc.; Controller manages
- communication between models and views.
2 What problems did I encounter
Yii Framework is a popular PHP framework that draws on Ruby on RailsActiveRecord(
AR) concept.
table in the database can use the
AR class to conveniently perform addition, deletion, modification and query operations.
models.
AR is just DAO (data access layer), not the Model layer.
Almost all of our business is placed in the Controller:Make various logical judgments on the forms submitted by users, perform calculations, instantiate AR and store the data...
Because there will be multipleaction in a Controller, each
action has such business processing.
Controller, like condoms, should be as thin as possible.
Its responsibility shouldJust accept user input and then immediately forward it to other classes for processing.
In this way, the Controller is only responsible for providing different interfaces, so that we can separate the business logic, and the separated business can be easily reused. Who will handle this separated part of the business? The answer should beModel.
3 The responsibility of View
View is relatively clear, it is responsible for display.
Everything that has nothing to do with the display interface should not appear in the view.
Therefore, generallyshould not have complex judgment statements or complex calculation processes in View .
You can have simple loop statements and formatting statements. For example, the text list on the blog homepage is a kind of loop. For PHP web applications,HTML is the main content in View.
Viewshould never call the Model writing method.
In other words, View only reads data from Model but does not rewrite Model.
So we say that View and Model are inseparable.
Furthermore, $_GET
and $_POST
are not directly accessed in View and should be passed to View by Controller.
In addition, View generally does not have any content to prepare data processing, such as querying the database, etc.
These are usually placed in the Controller and passed to the view in the form of variables.
In other words, the data to be used in the view is a variable .
4 Responsibilities of Model
For Model, the most important thing is to save and output information. For example, the Post class must have a
title attribute used to save the title of the blog post, and must have a delete operation. These are all the contents of the Model.
Data, behavior, and methods are the main contents of Model. In actual work,
Model has the largest amount of code in MVC. Model is the most complex part of the logic, because the business logic of the application must also be expressed here.
Pay attention to distinguishing Model from Controller.
Model handles business logic, and Controller simply coordinates the relationship between Model and View. As long as it is related to the business, it should be placed in the Model.
Data verification, publicconstants and variables should all be placed in the model layer,In other words, attributes or methods that may be reused should be placed in the model layer, defined once and used everywhere.
Model should not access request, session and other environment data, these should be injected by the Controller.
Good design should be
fat Model and thin Controller.
5 Responsibilities of ControllerFor
Controller, it mainly responds to user requests and decides what view to use, which needs to be prepared What data is used to display . Therefore,
The access code for request should be placed in the Controller , such as $_GET,
$_POST
etc. Controller should be limited to obtaining user request data,
should not perform any operations or preprocessing on data, this should be placed inside the Model. For data writing operations, the method of the Model class must be called to complete.
In response to user requests, view rendering is called.
In addition, generally
There should be no HTML code or other presentation layer things, this should be the content of the View.
6 EnlightenmentThere is this paragraph in the official documentation of Yii Framework:
In a well-designed MVC application, controllers are often very thin, containing probably only a few dozen lines of code; while models are very fat, containing most of the code responsible for representing and manipulating the data.In short,Rich Model is Better
.
The above is the detailed content of Examples to explain the meaning and division of responsibilities of MVC architecture. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP implements MVC architecture: basic principles and applications MVC (Model-View-Controller) is a common software design pattern. It is a way to divide an application into three core components (model, view, controller). The main purpose of this pattern is to separate code into independent building blocks to enhance maintainability, scalability, and reusability of development. In web development, MVC has become one of the most popular design patterns. PHP is one of the popular languages for web development.

How to implement a secure MVC architecture through the PHP8 framework. With the rapid development of the Internet, Web applications play an increasingly important role in our lives. However, as the complexity of web applications increases, security issues have become a critical issue that cannot be ignored. In order to protect the data security of applications and users, we need to adopt appropriate architecture and technical means to ensure the security of applications. In PHP development, adopting MVC architecture is a common practice. In the PHP8 framework, we can use

How to build an effective MVC architecture in the PHP8 framework MVC (Model-View-Controller) is a common software design pattern used to provide an effective organizational structure for applications. In PHP development, the MVC pattern is an important tool for developers to improve code maintainability and scalability. This article will introduce how to build an effective MVC architecture in the PHP8 framework. 1. Framework selection Choosing a suitable framework is the first step in building an MVC architecture. PHP8 currently has many

In Web development, the MVC architecture pattern is often used in the development of Web applications. In PHP development, the MVC architecture has been widely used, providing an effective solution for the development of Web applications. This article will introduce the implementation of the MVC architecture in PHP and its answers to frequently asked questions. 1. Introduction to MVC architectural pattern MVC is an architectural pattern for software development. It mainly consists of three components: Model, View and Controller.

MVC Architecture Analysis - Understanding the Basic Principles of Web Applications MVC (Model-View-Controller) architecture is a software design pattern commonly used to build Web applications. It divides the application into three basic components: Model, View and Controller. Each part is responsible for different functions and works together to make the application clearer, maintainable and scalable. Model A model is an application

In modern web development, using the MVC framework can greatly improve development efficiency and code maintainability. CakePHP is a PHP framework based on the MVC design pattern. Its ease of use and flexibility make it loved by many developers. In this article, we will introduce how to use the CakePHP framework to develop web applications. Preparation work Before using CakePHP, you need to install the following software: PHP5.6 or higher MySQL5.5 or higher Apache server

How to implement a scalable and maintainable MVC architecture in the PHP8 framework Introduction: With the continuous development of web applications, the MVC (Model-View-Controller) architecture has become a widely adopted design pattern. It can help developers separate application logic, views and data, improving code scalability and maintainability. In this article, we will introduce how to implement an extensible and maintainable MVC architecture in the PHP8 framework. 1. Understand the MVC architecture. The MVC architecture consists of three main components: Model (M

Yii framework adopts an MVC architecture and enhances its flexibility and scalability through components, modules, etc. 1) The MVC mode divides the application logic into model, view and controller. 2) Yii's MVC implementation uses action refinement request processing. 3) Yii supports modular development and improves code organization and management. 4) Use cache and database query optimization to improve performance.
