Backend Development 7 min read

Understanding MVC: Common Pitfalls and Proper Separation of Concerns in Yii Framework

The article explains the MVC design pattern, describes how the author mistakenly packed all business logic into Yii controllers leading to massive, unmaintainable code, and outlines the correct responsibilities of Model, View, and Controller to achieve thin controllers and rich models.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding MVC: Common Pitfalls and Proper Separation of Concerns in Yii Framework

1. What is MVC

MVC (Model-View-Controller) is a design pattern that separates business logic from the user interface, allowing developers to modify each component independently.

In MVC, the Model represents data and business rules; the View contains UI elements; the Controller manages communication between Model and View.

Implementations exist in many languages, e.g., J2EE uses JSP for View, Servlets/Struts for Controller, and entity beans for Model.

2. Problems Encountered

In a Yii project, the author mistakenly placed all business logic inside Controller action methods, resulting in thousands of lines per Controller and code that could not be reused.

Yii’s ActiveRecord ( AR ) was treated as the Model layer, but AR is merely a DAO; the real Model should contain business logic.

Because each Controller contains many action methods handling form submissions, the code became bloated and could not easily accept SOAP XML requests for an API.

3. Responsibilities of the View

The View is solely responsible for presentation; it should not contain complex logic, calculations, or direct access to request data.

All data needed by the View must be passed from the Controller, and the View should only read data from the Model.

In PHP web applications, HTML is the main content of the View, and the View should never call Model write methods or access $_GET / $_POST directly.

4. Responsibilities of the Model

The Model’s main job is to store and output information, encapsulating data, behavior, and methods.

It should contain validation, constants, reusable attributes, and never access request, session, or other environment data directly; such data should be injected by the Controller.

Thus a "fat Model, thin Controller" design is recommended.

5. Responsibilities of the Controller

The Controller handles user requests, selects the appropriate View, and prepares data by delegating to the Model.

It may access $_GET , $_POST , or request , but must not perform business logic or render HTML itself.

Data‑writing operations should be performed by Model methods, while the Controller merely forwards user input to the Model.

6. Takeaways

The official Yii documentation states: "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, a "Rich Model is Better" approach leads to more maintainable and reusable code.

backenddesign patternsMVCPHPYiiModel-View-Controller
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.