phpPHP
Module 33: PHP MVC ArchitectureAdvanced55 min read

PHP MVC Architecture: Front Controller, Router, Services & Repositories

Structure scalable PHP apps using the Front Controller pattern (public/index.php), Router, Controllers, Services, Repositories, and Views.

Key Takeaways & Core Concepts
  • Front Controller (`public/index.php`): Single entry point routing all requests.
  • Controller: Parses HTTP input, delegates to Services, returns View/JSON. Keep controllers thin!
  • Service Layer: Business rules and transactions.
  • Repository / Model: Database persistence via PDO.

PHP MVC Architecture: Front Controller, Router, Services & Repositories

### 1. Separation of Concerns in MVC

  • Front Controller (`public/index.php`): Single entry point routing all requests.
  • Controller: Parses HTTP input, delegates to Services, returns View/JSON. Keep controllers thin!
  • Service Layer: Business rules and transactions.
  • Repository / Model: Database persistence via PDO.
1. Minimal Front-Controller Router
1<?php
2$routes = ['GET /courses' => 'CourseController@index', 'POST /login' => 'AuthController@login'];
3$method = 'GET'; $uri = '/courses';
4$handler = $routes["$method $uri"] ?? 'ErrorController@notFound';
5echo "Resolved handler: $handler\n";
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
5 lines
Note: Maps HTTP method + URI to a controller action.
Practice & AssessmentPHP Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)