I’ve created a complete working MVC OOP PHP project for you in the canvas 👉
This is a basic but real structure you can run and extend.
🚀 How to Run
- Create folder structure:
project/
app/
controllers/
models/
views/
core/
public/
- Move code into proper files (I kept it in one file for simplicity — you should split it)
- Run using:
- XAMPP / WAMP →
localhost/project/public - Or PHP built-in server:
php -S localhost:8000
⚠️ Important Improvements (Next Step)
Right now it’s basic, but for real production you should:
🔐 Add Database (MySQL)
- Use PDO
- Store users in DB (not array)
🧠 Add Features
- Sessions (login system)
- Middleware (auth check)
- Validation
- Error handling
📦 Use Autoloading
spl_autoload_register(function($class){
require_once \"../app/$class.php\";
});
<?php
// =============================
// SIMPLE MVC OOP PHP PROJECT
// =============================
// --------- FOLDER STRUCTURE ---------
// /project
// /app
// /controllers
// /models
// /views
// /core
// /config
// public/index.php
// =============================
// CORE: Router
// =============================
class Router {
public function route() {
$url = $_GET['url'] ?? 'home/index';
$url = explode('/', $url);
$controllerName = ucfirst($url[0]) . 'Controller';
$method = $url[1] ?? 'index';
require_once "app/controllers/$controllerName.php";
$controller = new $controllerName();
call_user_func([$controller, $method]);
}
}
// =============================
// CORE: Controller
// =============================
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";
}
}
// =============================
// MODEL: User
// =============================
class User {
private $users = [];
public function register($name, $password) {
$this->users[$name] = password_hash($password, PASSWORD_DEFAULT);
}
public function login($name, $password) {
if (isset($this->users[$name]) && password_verify($password, $this->users[$name])) {
return true;
}
return false;
}
}
// =============================
// CONTROLLER: Home
// =============================
class HomeController extends Controller {
public function index() {
$this->view('home');
}
}
// =============================
// CONTROLLER: Auth
// =============================
class AuthController extends Controller {
private $user;
public function __construct() {
$this->user = new User();
}
public function register() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$this->user->register($_POST['name'], $_POST['password']);
echo "Registered Successfully";
}
$this->view('register');
}
public function login() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($this->user->login($_POST['name'], $_POST['password'])) {
echo "Login Success";
} else {
echo "Login Failed";
}
}
$this->view('login');
}
}
// =============================
// VIEW: home.php
// =============================
?>
<h1>Welcome to MVC App</h1>
<a href="?url=auth/register">Register</a>
<a href="?url=auth/login">Login</a>
<?php
// =============================
// VIEW: register.php
// =============================
?>
<form method="POST">
<input type="text" name="name" placeholder="Name">
<input type="password" name="password" placeholder="Password">
<button type="submit">Register</button>
</form>
<?php
// =============================
// VIEW: login.php
// =============================
?>
<form method="POST">
<input type="text" name="name" placeholder="Name">
<input type="password" name="password" placeholder="Password">
<button type="submit">Login</button>
</form>
<?php
// =============================
// PUBLIC: index.php (ENTRY POINT)
// =============================
require_once '../core/Router.php';
require_once '../core/Controller.php';
$router = new Router();
$router->route();






