πŸ“– Displaying a Registration Form with PHP MVC

πŸ” What You’re Building

You’ll set up a PHP file that acts as the app’s entry point (register.php). It loads a controller method, which then loads a simple HTML form. This step lays the groundwork for validating and saving form data later.

πŸ“ Project Structure

This structure shows how the form request flows from user input to view rendering, using layout partials:

project-root/
β”œβ”€β”€ controllers/
β”‚   └── UserController.php        ← logic for loading the form
β”œβ”€β”€ views/
β”‚   β”œβ”€β”€ partials/
β”‚   β”‚   β”œβ”€β”€ header.php            ← shared HTML header and navigation
β”‚   β”‚   └── footer.php            ← shared footer and closing tags
β”‚   └── profile/
β”‚       └── create.php            ← displays the registration form
└── register.php                  ← entry point (user request lands here)

This layout demonstrates a modular view system where page-specific content is wrapped with shared header and footer templates for consistency.

πŸ› οΈ Step 1: Create the Entry Point

register.php is the public file the browser loads. It passes control to a method in your controller.

<?php
require_once 'controllers/UserController.php';

$controller = new UserController();
$controller->register();

This is the entire job of the entry file: load the controller and ask it what to do.

🧠 Step 2: Add the Controller Method

In controllers/UserController.php, define the controller class and its register() method.

<?php
class UserController {
    public function register() {
        require 'views/profile/create.php';
    }
}

This method simply loads the view that shows the registration form. It doesn't handle any logic yet β€” that's coming later.

πŸ–ΌοΈ Step 3: Build the View

πŸ’‘ Tip: Action views like show.php, edit.php, or create.php represent distinct interface behaviors within a feature folder (e.g., views/profile/).

In views/profile/create.php, build the registration form using layout partials. This keeps your view modular and consistent across the application.

<?php include 'views/partials/header.php'; ?>

<h2>Create Profile</h2>

<form method="POST" action="register.php">
  <div>
    <label for="name">Name</label>
    <input id="name" name="name">
  </div>

  <div>
    <label for="email">Email</label>
    <input type="email" id="email" name="email">
  </div>

  <button type="submit">Register</button>
</form>

<?php include 'views/partials/footer.php'; ?>

This version removes all layout markup from the view and loads shared components instead:

  • views/partials/header.php contains the opening HTML, page head, and navigation
  • views/partials/footer.php closes the page and includes any global scripts or footer content

Using partials prepares your app for form reuse and makes future updates easier across pages.

βœ… Recap

  • User requests register.php
  • Entry point creates a controller
  • Controller loads the view
  • View displays a basic registration form

Next, you’ll modify the controller to handle form submissions and display errors when needed.

πŸ’‘ Next Step: Validate user input and return helpful error messages without reloading the form unnecessarily.

Last updated: August 6, 2025 at 3:11 PM