📖 Scenario: Imagine you are building a simple web application to show a list of books. You want to organize your code so it is easy to manage and understand. The MVC (Model-View-Controller) pattern helps you do this by separating your code into three parts: data (Model), user interface (View), and control logic (Controller).
🎯 Goal: You will create a basic PHP program that uses MVC architecture to display a list of books. You will set up the data, configure a controller, create a view to show the books, and finally display the output.
📋 What You'll Learn
Create a Model to hold book data
Create a Controller to handle the logic
Create a View to display the books
Use MVC structure to separate code clearly
💡 Why This Matters
🌍 Real World
MVC is used in many web applications to keep code organized and easy to update. For example, online stores use MVC to manage products, user views, and actions separately.
💼 Career
Understanding MVC is important for PHP developers working on web projects. It helps you write clean, maintainable code and collaborate with others.
Progress0 / 4 steps
1
Create the Model with book data
Create a PHP array called $books with these exact entries: 'The Great Gatsby', '1984', 'To Kill a Mockingbird'.
PHP
Hint
Use a PHP array with the exact book titles inside square brackets.
2
Create the Controller to manage books
Create a function called getBooks() that returns the $books array.
PHP
Hint
Use the global keyword inside the function to access $books.
3
Create the View to display books
Create a function called showBooks() that calls getBooks() and uses a foreach loop with variable $book to print each book inside an HTML <li> element.
PHP
Hint
Use echo to print HTML list items inside the loop.
4
Display the books using MVC
Call the showBooks() function to display the list of books.
PHP
Hint
Just call showBooks() to print the list.
Practice
(1/5)
1. In the MVC architecture, which component is responsible for handling user input and updating the model?
easy
A. Controller
B. Model
C. View
D. Database
Solution
Step 1: Understand MVC roles
The Model manages data, the View displays data, and the Controller handles user input.
Step 2: Identify the component for user input
The Controller receives user actions and updates the Model accordingly.
Final Answer:
Controller -> Option A
Quick Check:
Controller handles input [OK]
Hint: Controller connects user actions to data changes [OK]
Common Mistakes:
Confusing Model with Controller
Thinking View handles input
Assuming Database is part of MVC
2. Which of the following is the correct way to describe the role of the Model in MVC?
easy
A. It processes user input and updates views.
B. It manages the data and business logic.
C. It displays data to the user.
D. It handles routing and URLs.
Solution
Step 1: Recall Model's responsibility
The Model stores and manages data and business rules.
Step 2: Match description to Model
Only It manages the data and business logic. correctly states that the Model manages data and logic.
Final Answer:
It manages the data and business logic. -> Option B
Quick Check:
Model manages data and logic [OK]
Hint: Model is about data and rules, not display or input [OK]
Common Mistakes:
Mixing up Model and View roles
Thinking Controller manages data
Confusing routing with Model
3. Given this PHP MVC snippet:
<?php
class Controller {
public function show() {
$data = Model::getData();
View::render($data);
}
}
class Model {
public static function getData() {
return ['name' => 'Alice'];
}
}
class View {
public static function render($data) {
echo 'Hello, ' . $data['name'];
}
}
$controller = new Controller();
$controller->show();
?>
What is the output when this code runs?
medium
A. Hello, name
B. Array
C. Hello, Alice
D. Error: Undefined index
Solution
Step 1: Trace data flow in code
The Controller calls Model::getData(), which returns ['name' => 'Alice'].
Step 2: Check View rendering
View::render() echoes 'Hello, ' plus the value of $data['name'], which is 'Alice'.
Final Answer:
Hello, Alice -> Option C
Quick Check:
Output = 'Hello, Alice' [OK]
Hint: Follow data from Model to View for output [OK]
Common Mistakes:
Printing the array directly
Using wrong array key
Confusing static method calls
4. Identify the error in this MVC PHP code snippet:
<?php
class Controller {
public function display() {
$data = Model::fetchData();
View::show($data);
}
}
class Model {
public static function getData() {
return ['age' => 30];
}
}
class View {
public static function show($data) {
echo 'Age: ' . $data['age'];
}
}
$controller = new Controller();
$controller->display();
?>
medium
A. Model::fetchData() method does not exist
B. View::show() method is missing
C. Controller class is not instantiated
D. Array key 'age' is incorrect
Solution
Step 1: Check method calls in Controller
Controller calls Model::fetchData(), but Model only has getData() method.
Step 2: Verify Model methods
Model::fetchData() does not exist, causing a fatal error.
Final Answer:
Model::fetchData() method does not exist -> Option A
Quick Check:
Model::fetchData() method does not exist [OK]
Hint: Match method names exactly between classes [OK]
Common Mistakes:
Assuming method names are interchangeable
Ignoring case sensitivity
Overlooking missing methods
5. You want to add a feature in your PHP MVC app to display a list of users. Which MVC components should you update and how?
hard
A. Update Model to display list, View to fetch users, Controller to store data
B. Update View to fetch users, Controller to display list, Model to handle input
C. Update Controller to fetch users, Model to display list, View to handle input
D. Update Model to fetch users, Controller to pass data, View to display list
Solution
Step 1: Assign responsibilities per MVC
The Model fetches data, Controller processes logic, View shows output.
Step 2: Map feature to components
Model fetches users, Controller gets data and sends to View, View displays user list.
Final Answer:
Update Model to fetch users, Controller to pass data, View to display list -> Option D
Quick Check:
Model=data, Controller=logic, View=display [OK]
Hint: Model gets data, Controller links, View shows it [OK]
Common Mistakes:
Letting View fetch data directly
Making Controller handle display
Confusing roles of MVC parts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.