What is MVC? The Pattern Behind Most Web Apps
General • Concepts Explained • 6 min read
MVC separates applications into Model, View, and Controller. Learn the pattern used by Django, Laravel, Spring, and Rails.
What is MVC? The Pattern Behind Most Web Apps
MVC is a design pattern used in most web frameworks. Django, Laravel, Ruby on Rails, Spring — they all use MVC. Understanding it helps you learn any framework faster.
What is MVC?
MVC stands for Model-View-Controller. It separates an application into three interconnected components:
- Model — Data and business logic
- View — What the user sees (UI)
- Controller — Handles user input and connects Model to View
How MVC Works
When a user visits a page:
- The Controller receives the request
- The Controller asks the Model for data
- The Model retrieves data from the database
- The Controller passes data to the View
- The View renders the HTML with the data
- The user sees the page
Example: Blog Application
// Model - handles data
class Post {
static findAll() {
return db.query('SELECT * FROM posts');
}
}
// View - renders HTML (EJS template)
// views/posts.ejs
// <% posts.forEach(post => { %>
// <h2><%= post.title %></h2>
// <% }) %>
// Controller - handles request
app.get('/posts', async (req, res) => {
const posts = await Post.findAll();
res.render('posts', { posts });
});
Benefits of MVC
- Separation of concerns — Each component has one job
- Team collaboration — Frontend and backend developers work independently
- Testability — Each component can be tested separately
- Reusability — Models can be used across different views
MVC Frameworks by Language
| Language | Framework |
|---|---|
| JavaScript | Express (with EJS/Pug) |
| Python | Django, Flask |
| PHP | Laravel, CodeIgniter |
| Ruby | Rails |
| Java | Spring MVC |
Note: MVC is a concept, not a strict rule. Different frameworks implement it slightly differently. Learn the pattern, and you can adapt to any framework.