π 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.phpcontains the opening HTML, page head, and navigationviews/partials/footer.phpcloses 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