Content · Web Development · PHP

Building a Simple MVC Application

A small MVC app can use a front controller, a dedicated service/model and a template.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

A small MVC app can use a front controller, a dedicated service/model and a template.

What It Does and When to Use It


Separate business logic, input handling and rendering at a clearly identified boundary. Controllers translate HTTP input to operations; domain services implement rules; templates render escaped output.

Requirements, Inputs and Expected Behavior


Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Avoid turning the controller into the place for SQL, validation and HTML construction.

Example 1: Basic Operation


Separate business logic, input handling and rendering at a clearly identified boundary.

<?php
final class UserRepository { public function find(int $id): array { return ['id'=>$id,'name'=>'Ava']; } }
final class UserController { public function __construct(private UserRepository $repo) {} public function show(int $id): array { return $this->repo->find($id); } }
echo json_encode((new UserController(new UserRepository()))->show(2), JSON_THROW_ON_ERROR);

Example 2: Behavior and Validation


Use the front controller to select responses while keeping routing separate from models.

<?php
$path=parse_url($_SERVER['REQUEST_URI']??'/',PHP_URL_PATH);
if ($path==='/health') { header('Content-Type: application/json'); echo json_encode(['status'=>'ok']); }
else { http_response_code(404); echo 'Not found'; }

Practical Example: Application Use


Render a trusted template path with controlled data; never derive template paths from user input.

<?php
function render(string $template, array $data): string {
    extract($data, EXTR_SKIP); ob_start();
    try { require $template; return (string)ob_get_clean(); }
    catch(Throwable $e) { ob_end_clean(); throw $e; }
}

Important Behavior and Edge Cases


The second example highlights: Use the front controller to select responses while keeping routing separate from models. The practical example adds: Render a trusted template path with controlled data; never derive template paths from user input.

Comparison with Related Approaches


ApproachWhat changes
Controller, service and template responsibilitiesControllers translate HTTP input to operations; domain services implement rules; templates render escaped output.
Choosing safelyTreat template names, route parameters and user data as separate trust domains.

Common Beginner Mistakes


MistakeCorrect approach
Assuming successful execution proves the result is usableCheck the return value, required extension, and observable output of the specific operation.
Ignoring runtime and environment differencesVerify PHP version, SAPI, operating system, service access, and permissions before deployment.
Skipping operational constraintsAvoid turning the controller into the place for SQL, validation and HTML construction.

Key Takeaways


  • A small MVC app can use a front controller, a dedicated service/model and a template.
  • Render a trusted template path with controlled data; never derive template paths from user input.
  • Treat template names, route parameters and user data as separate trust domains.
Share this resourceSend this page to someone who may find it useful.