Model-View-Controller
"Separate data (Model), presentation (View), and flow control (Controller). Each piece has one job — that's the entire idea."
CSE 135 — Full Overview | Review Questions
Three components, each with a single responsibility.
| Component | Responsibility | Knows About |
|---|---|---|
| Model | Data & business logic. Reads/writes database, validates data, enforces rules. | Database. Not how data is displayed. |
| View | Presentation. Takes data and renders it (HTML, JSON, etc.). | Data format. Not how data is stored. |
| Controller | Flow control. Receives requests, asks Model for data, passes results to View. | Both Model and View — the "traffic cop." |
What happens without MVC — and the benefits of splitting responsibilities.
Routing + SQL + HTML all mixed together. Untestable, unreusable, unmaintainable.
| Benefit | How MVC Helps |
|---|---|
| Testability | Test Model without HTML, test View without DB |
| Team Workflow | Frontend & backend devs don't collide |
| Reusability | Same Model → HTML, JSON, CSV |
| Maintainability | Change display? Edit View. Change schema? Edit Model. |
The server renders complete HTML — the browser just displays it.
GET /users)User.getAll())| Framework | Language | Template Engine |
|---|---|---|
| Express + EJS | Node.js | EJS, Pug, Handlebars |
| Laravel | PHP | Blade |
| Rails | Ruby | ERB, Haml |
| Django | Python | Django Templates, Jinja2 |
The server sends JSON — JavaScript renders everything in the browser.
<script> tagfetch('/api/users')| Framework | Approach |
|---|---|
| React | Component-based, virtual DOM |
| Vue | Reactive data binding, single-file components |
| Angular | Full framework with dependency injection, RxJS |
One backend, two paths — HTML for browsers, JSON for JavaScript clients.
Accept header to decide.
They're not competing — they operate at different layers.
You can have REST without MVC (a single-file API script), MVC without REST (a server-rendered app with no API), or both together (the most common real-world pattern).
Same concepts, different file names and conventions.
| Framework | Routes | Controller | Model | View / Template |
|---|---|---|---|---|
| Express (Node.js) | routes/users.js |
controllers/userController.js |
models/User.js |
views/users/index.ejs |
| Laravel (PHP) | routes/web.php |
app/Http/Controllers/UserController.php |
app/Models/User.php |
resources/views/users/index.blade.php |
| Django (Python) | urls.py |
views.py |
models.py |
templates/users/index.html |
| Rails (Ruby) | config/routes.rb |
app/controllers/users_controller.rb |
app/models/user.rb |
app/views/users/index.html.erb |
views.py. What others call a "View" is Django's templates/. The concepts are identical — only the names differ.
The same application built three different ways.
| Module | Approach | What the Server Returns |
|---|---|---|
| 06: Database Setup | PostgreSQL fundamentals | SQL: CREATE, INSERT, SELECT, UPDATE, DELETE |
| 07: Server-Side MVC | Full server rendering | Complete HTML pages (EJS / PHP templates) |
| 08: Client-Side MVC | SPA with JSON API | JSON data only; JS renders the DOM |
| 09: Adaptable MVC | Progressive enhancement | HTML or JSON based on Accept header |
8 sections of MVC fundamentals in one table.
| Concept | Key Points |
|---|---|
| MVC | Separates Model (data), View (presentation), Controller (flow). A pattern, not a framework. |
| Model | Data & business rules. Talks to DB. No knowledge of display. |
| View | Renders output (HTML, JSON, etc.). No knowledge of storage. |
| Controller | Receives input, coordinates Model & View. The "traffic cop." |
| Server-Side MVC | Server renders complete HTML. Best for content sites, SEO, progressive enhancement. Frameworks: Express+EJS, Laravel, Django, Rails. |
| Client-Side MVC | Server sends JSON, JS renders UI. Best for interactive apps. Frameworks: React, Vue, Angular. Requires JS. |
| Adaptable MVC | Same controller serves HTML or JSON via Accept header. Progressive enhancement: works without JS, enhanced with it. |
| REST vs MVC | REST = external API interface. MVC = internal code organization. Complementary, not competing. |