Here’s a concise guide covering the core principles, design patterns, and process to get you started.
π What is MVC?
MVC stands for Model-View-Controller:
- Model: Handles business logic and database interaction.
- View: Manages the presentation layer (HTML, templates).
- Controller: Handles user input, communicates between Model and View.
π Core Principles
- Separation of Concerns: Keep logic, presentation, and data access separate.
- Routing: Map URLs to controller actions.
- Autoloading (PSR-4): Load classes automatically using namespaces.
- Dependency Injection: Avoid hard-coded dependencies; use constructor or container.
- Single Responsibility Principle (SOLID): Each class has one job.
- Security: Sanitize inputs, use prepared statements, CSRF protection, etc.
- Templating: Avoid mixing PHP logic with HTML (use template engines like Twig or Blade).
- Modern PHP Standards: Use PHP 8+, type hints, and attributes.
ποΈ Basic Folder Structure
/your-app
β
βββ /app
β βββ /Controllers
β βββ /Models
β βββ /Views
β βββ /Core <- Router, Base Controller, etc.
β
βββ /public
β βββ index.php <- Entry point (Front Controller)
β
βββ /config
βββ /routes
βββ composer.json
π οΈ Step-by-Step Setup
1. Set Up Entry Point (public/index.php)
require_once __DIR__ . '/../vendor/autoload.php';
use App\Core\Router;
$router = new Router();
$router->dispatch($_SERVER['REQUEST_URI']);
2. Router (app/Core/Router.php)
- Parse URL
- Match with defined routes
- Load controller & call method
namespace App\Core;
class Router {
public function dispatch($uri) {
// parse URI, match routes, call controller
}
}
3. Controller Example
namespace App\Controllers;
use App\Core\Controller;
class HomeController extends Controller {
public function index() {
$this->view('home/index', ['title' => 'Home']);
}
}
4. View Loader (Controller.php Base Class)
public function view($view, $data = []) {
extract($data);
require "../app/Views/$view.php";
}
5. Model Example
namespace App\Models;
use PDO;
class User {
public static function all() {
// return all users using PDO
}
}
6. Autoloading with Composer
composer.json
{
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}
Then run:
composer dump-autoload
β Modern Enhancements
- Use dotenv for config (
vlucas/phpdotenv) - Use a DI container (like PHP-DI)
- Implement Middleware for auth, logging
- Use PDO with prepared statements or ORM like Eloquent
- Add unit testing (PHPUnit)
- Use Twig or Blade templating
- Add error handling and logging
π§ Next Steps
- Set up a basic working app with routes, controller, and view.
- Expand the router to handle parameters and method-based routing.
- Connect models with a database using PDO or ORM.
- Add templating, error handling, and helpers.
- Gradually modularize (middlewares, services, auth, etc.)
