Skip to main content

Overview

The TeacherController handles teacher record management including listing, creation, and deletion. All operations require admin privileges. File Location: app/controllers/TeacherController.php Dependencies:
  • Teacher model
  • Auth helper

Methods

index()

Displays list of all teachers. Authorization: Admin only Behavior:
  • Retrieves all teacher records from database
  • Renders teacher listing view
Code Example:
public function index()
{
    Auth::admin();

    $teacherModel = new Teacher();
    $teachers = $teacherModel->all();

    require __DIR__ . '/../views/teachers/index.php';
}
View Location: app/views/teachers/index.php

create()

Displays teacher creation form and handles teacher registration. Authorization: Admin only
name
string
required
Teacher’s full name (POST request)
email
string
required
Teacher’s email address (POST request)
password
string
required
Teacher’s password (POST request)
Behavior:
  • GET request: Displays creation form
  • POST request: Creates new teacher and redirects
Code Example:
public function create()
{
    Auth::admin();

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $teacherModel = new Teacher();
        $teacherModel->create(
            $_POST['name'],
            $_POST['email'],
            $_POST['password']
        );

        header("Location: /school_management/public/teachers");
        exit;
    }

    require __DIR__ . '/../views/teachers/create.php';
}
Response:
  • GET: Renders creation form at app/views/teachers/create.php
  • POST: Redirects to /teachers

delete()

Deletes a teacher record. Authorization: Admin only
id
integer
required
Teacher ID to delete (from GET query parameter)
Behavior:
  1. Validates admin authorization
  2. Deletes teacher from database
  3. Redirects to teacher listing
Code Example:
public function delete()
{
    Auth::admin();

    $teacherModel = new Teacher();
    $teacherModel->delete($_GET['id']);

    header("Location: /school_management/public/teachers");
    exit;
}
Response: Redirects to /teachers

Usage Examples

Creating a Teacher

// POST /teachers/create
$_POST = [
    'name' => 'Dr. Sarah Williams',
    'email' => 'sarah.williams@school.com',
    'password' => 'securepass456'
];

Deleting a Teacher

// GET /teachers/delete?id=3
$_GET['id'] = 3;

Security Notes

  • All methods enforce admin-only access via Auth::admin()
  • Passwords are processed by the Teacher model (should be hashed)
  • No session or authentication data returned in responses

Build docs developers (and LLMs) love