Table of Contents
4 Responsibilities of Model" >4 Responsibilities of Model
Home Backend Development PHP Tutorial Examples to explain the meaning and division of responsibilities of MVC architecture

Examples to explain the meaning and division of responsibilities of MVC architecture

Jan 07, 2022 pm 02:09 PM
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.

As a result, each

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.

Examples to explain the meaning and division of responsibilities of MVC architecture

1 What exactly is MVC

Model-View-Controller (MVC) is a type of

Design framework (design pattern) .

The

goal 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.
MVC is implemented in various programming languages. For example, in J2EE application development,

View may be implemented by jsp; Controller is a servlet, now generally implemented with Struts; Model It is implemented by an entity Bean.

2 What problems did I encounter

Yii Framework is a popular PHP framework that draws on Ruby on Rails

ActiveRecord(AR) concept.

Every

table in the database can use the AR class to conveniently perform addition, deletion, modification and query operations.

It treats AR as a Model and recommends placing it under a directory named

models.

So, after I automatically generated the AR corresponding to the table, I took it for granted that I already had the Model layer.

Actually,

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 multiple

action in a Controller, each action has such business processing.

Finally, I found that my Controller code had exceeded 1000 lines.

Suddenly one day, the leader said that our system should open APIs for existing old systems to call and provide third-party interfaces.

The third party only needs to give a parameter, and the system only gives a result value. It does not care about the business processing.

The bad thing is that the Controller has already implemented those services, but it accepts form submissions. How can it also accept SOAP xml documents?

Controller, like condoms, should be as thin as possible.

Its responsibility should

Just 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 be

Model.

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, generally

should 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.

View

should 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 Controller

For

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, $_POSTetc. 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 Enlightenment

There 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP implements MVC architecture: basic principles and applications PHP implements MVC architecture: basic principles and applications Jun 18, 2023 pm 01:13 PM

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 How to implement a secure MVC architecture through the PHP8 framework Sep 11, 2023 am 08:21 AM

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 PHP8 framework How to build an effective MVC architecture in PHP8 framework Sep 12, 2023 pm 03:18 PM

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

Implementation of MVC architecture in PHP and answers to frequently asked questions Implementation of MVC architecture in PHP and answers to frequently asked questions Jun 08, 2023 pm 02:19 PM

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 architecture analysis -- understanding the basic principles of Web applications Sep 08, 2023 am 10:48 AM

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

How to use CakePHP framework in php? How to use CakePHP framework in php? May 31, 2023 pm 02:51 PM

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 scalable and maintainable MVC architecture in PHP8 framework How to implement scalable and maintainable MVC architecture in PHP8 framework Sep 11, 2023 am 08:49 AM

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's Architecture: MVC and More Yii's Architecture: MVC and More Apr 11, 2025 pm 02:41 PM

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.

See all articles