Skip to main content

Overview

The StudentController manages student records including creation, listing, deletion, and search functionality. All methods require admin authentication. File Location: app/controllers/StudentController.php Dependencies:
  • Student model
  • Auth helper

Methods

index()

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

    $students = $this->model->getAll();
    require __DIR__ . '/../views/students/index.php';
}
View Location: app/views/students/index.php

create()

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

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

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

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

delete()

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

    $this->model->delete($_GET['id']);
    header("Location: /school_management/public/students");
    exit;
}
Response: Redirects to /students
Searches for students by keyword. Authorization: Admin only
q
string
Search keyword (searches name or email)
Behavior:
  • If keyword provided: Returns filtered results
  • If no keyword: Returns all students
  • Uses the same view as index()
Code Example:
public function search()
{
    Auth::admin();

    $studentModel = new Student();

    $keyword = $_GET['q'] ?? '';

    if ($keyword !== '') {
        $students = $studentModel->search($keyword);
    } else {
        $students = $studentModel->getAll();
    }

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

Usage Examples

Creating a Student

// POST /students/create
$_POST = [
    'name' => 'Alice Johnson',
    'email' => 'alice@example.com',
    'password' => 'securepassword123'
];

Searching Students

// GET /students/search?q=alice
$_GET['q'] = 'alice';

Deleting a Student

// GET /students/delete?id=5
$_GET['id'] = 5;

Build docs developers (and LLMs) love