MVC (Model-View-Controller) is a widely used design pattern in web development that helps to separate the presentation layer from the business logic and data access layer. This makes the code more organized, maintainable, and easier to test. Most MVC frameworks, such as Laravel, CodeIgniter, and Ruby on Rails, come with their own set of rules and conventions. However, what if you want to build an MVC architecture without relying on any framework? In this tutorial, we will guide you through the process of creating a framework-free MVC application from scratch.
Step 1: Setting Up the Project
The first step is to set up the project structure. Create a new folder for your project and name it whatever you like. Inside this folder, create three subfolders: "models", "views", and "controllers". These folders will be used to store the respective components of the MVC architecture.
Step 2: Creating the Model
The model is responsible for handling the data of the application. In our tutorial, we will create a simple user model to demonstrate the functionality of the MVC pattern. Create a new file inside the "models" folder and name it "User.php". In this file, we will define a class called "User" and create a function to retrieve user data from a database.
```
class User {
// Database connection
private $conn;
// User properties
public $id;
public $name;
public $email;
// Constructor with database connection
public function __construct($db) {
$this->conn = $db;
}
// Get user data from database
public function getUserData() {
// Create query
$query = "SELECT * FROM users WHERE id = :id";
// Prepare statement
$stmt = $this->conn->prepare($query);
// Bind parameter
$stmt->bindParam(':id', $this->id);
// Execute query
$stmt->execute();
// Fetch user data
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// Set user properties
$this->name = $row['name'];
$this->email = $row['email'];
}
}
```
Step 3: Creating the Controller
The controller is responsible for handling user requests and calling the appropriate model and view. Create a new file inside the "controllers" folder and name it "UserController.php". In this file, we will define a class called "UserController" and create a function to retrieve user data from the model and pass it to the view.
```
class UserController {
// Database connection
private $conn;
// User model
private $user;
// Constructor with database connection
public function __construct($db) {
$this->conn = $db;
$this->user = new User($db);
}
// Get user data and pass it to the view
public function index() {
// Set user id
$this->user->id = 1;
// Get user data from model
$this->user->getUserData();
// Pass data to view
$data = [
'user' => $this->user
];
// Load view
require_once 'views/user.php';
}
}
```
Step 4: Creating the View
The view is responsible for displaying the data to the user. Create a new file inside the "views" folder and name it "user.php". In this file, we will display the user's name and email.
```
<h1>Welcome, <?php echo $data['user']->name; ?></h1>
<p>Your email is: <?php echo $data['user']->email; ?></p>
```
Step 5: Routing
Now that we have created the model, controller, and view, we need to connect them together. In a traditional MVC framework, this is done through routing. However, since we are not using any framework, we will create a simple routing system using the .htaccess file. Create a new file called ".htaccess" in your project folder and add the following code:
```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
```
This will redirect all requests to the index.php file and pass the request URL as a parameter. Now, create a file named "index.php" in your project folder and add the following code: