JINSI YA KUTENGENEZA MVC STRUCTURE KWA PHP PROJECTS
Model – handles data, database interactions
View – handles presentation, HTML templates
Controller – handles application logic, requests, responses
Faida:
Clean separation of concerns
Easy to maintain and scale
Reusable components
⚙️ 2. Project Folder Structure
project_root/
│
├── app/
│ ├── controllers/
│ │ └── UserController.php
│ ├── models/
│ │ └── User.php
│ └── views/
│ └── user_view.php
│
├── public/
│ └── index.php
│
├── core/
│ ├── App.php
│ └── Controller.php
│
└── config/
└── config.php
public/index.php – front controller (entry point)
core/ – framework core (router, base controller)
app/controllers/ – request handling
app/models/ – data and DB interaction
app/views/ – presentation layer
🧩 3. Example Configuration (config/config.php)
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'mvc_db');
define('DB_USER', 'root');
define('DB_PASS', '');
🧩 4. Core Application (core/App.php)
<?php
class App {
protected $controller = 'UserController';
protected $method = 'index';
protected $params = [];
public function __construct(){
$url = $this->parseUrl();
// Controller
if(file_exists('../app/controllers/' . $url[0] . '.php')){
$this->controller = $url[0];
unset($url[0]);
}
require_once '../app/controllers/' . $this->controller . '.php';
$this->controller = new $this->controller;
// Method
if(isset($url[1])){
if(method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}
}
// Parameters
$this->params = $url ? array_values($url) : [];
call_user_func_array([$this->controller, $this->method], $this->params);
}
private function parseUrl(){
if(isset($_GET['url'])){
return explode('/', filter_var(rtrim($_GET['url'],'/'), FILTER_SANITIZE_URL));
}
}
}
🧩 5. Base Controller (core/Controller.php)
<?php
class Controller {
public function model($model){
require_once '../app/models/' . $model . '.php';
return new $model();
}
public function view($view, $data = []){
require_once '../app/views/' . $view . '.php';
}
}
🧩 6. Example Controller (app/controllers/UserController.php)
<?php
class UserController extends Controller {
public function index(){
$userModel = $this->model('User');
$users = $userModel->getAllUsers();
$this->view('user_view', ['users' => $users]);
}
}
🧩 7. Example Model (app/models/User.php)
<?php
class User {
private $pdo;
public function __construct(){
$this->pdo = new PDO("mysql:host=localhost;dbname=mvc_db;charset=utf8mb4","root","");
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function getAllUsers(){
$stmt = $this->pdo->query("SELECT * FROM users");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
🧩 8. Example View (app/views/user_view.php)
<!DOCTYPE html>
<html>
<head>
<title>Users List</title>
</head>
<body>
<h2>Users</h2>
<ul>
<?php foreach($data['users'] as $user): ?>
<li><?php echo htmlspecialchars($user['username']); ?></li>
<?php endforeach; ?>
</ul>
</body>
</html>
⚡ 9. Front Controller (public/index.php)
<?php
require_once '../core/App.php';
require_once '../core/Controller.php';
$app = new App();
🔑 10. Best Practices
Separate concerns – keep logic, presentation, and data separate.
Sanitize output – use htmlspecialchars() in views.
Use PDO with prepared statements – secure database access.
Organize folder structure – controllers, models, views, core.
Implement routing – clean URLs like /user/index.
✅ 11. Hitimisho
MVC structure makes PHP projects maintainable, scalable, and secure.
Allows easy expansion with authentication, multi-user systems, and secure file handling.
Combine with sessions, RBAC, CSRF protection for full security.
🔗 Tembelea:
👉 https://www.faulink.com/
Kwa mafunzo zaidi ya PHP, MVC architecture, na best practices za web development.