MVC Master

Módulo 5: Profesores VIP (CRUD Premium)

Vamos a subir de nivel. Te enseñaremos a crear una Interfaz Premium Dividida para los Docentes, aplicando clases visuales modernas y protegiendo la inserción de datos únicamente a los docentes con rol Administrador. ¡Tu Demo ya usa este código!

5.1: El Modelo de Base de Datos

app/Models/ProfesorModelo.php
<?php
namespace App\Models;

class ProfesorModelo extends \App\Core\ModeloBase {
    protected $tabla = 'profesores';
    
    public function insertar($nombre, $especialidad) {
        $sql = "INSERT INTO {$this->tabla} (nombre, especialidad) VALUES (:n, :e)";
        return $this->db->prepare($sql)->execute(['n'=>$nombre, 'e'=>$especialidad]);
    }
}

5.2: El Controlador Dividido (Index y Crear)

Añade estas funciones a tu DemoController. Observa cómo usamos las protecciones de SESSION para que un Invitado jamás inserte un Profesor falso en el Servidor.

app/Controllers/DemoController.php (Añadir)
    public function profesores() {
        $modelo = new \App\Models\ProfesorModelo(); 
        $this->render('profesores/index', ['titulo' => 'Gestión de Docentes', 'profesores' => $modelo->all()]);
    }

    public function crearProfesor() {
        $this->render('profesores/crear', ['titulo' => 'Reclutar Docente']);
    }

    public function guardarProfesor() {
        if($_SESSION['usuario_rol'] === 'ADMINISTRADOR') {
            (new \App\Models\ProfesorModelo())->insertar($_POST['nombre'], $_POST['espec']);
            header("Location: " . BASE_URL . "/demo/profesores?msg=Realizado!"); exit;
        }
    }

5.3: El Directorio VIP (Listado Ancho)

Crea el archivo index central con estilos ultra modernos.

app/Views/profesores/index.php
<div class="card border-0 shadow-sm p-4" style="border-radius: 15px;">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">Directorio Docente VIP</h2>
        <a href="/demo/profesores/crear" class="btn btn-primary fw-bold">Reclutar Docente</a>
    </div>

    <table class="table table-hover align-middle bg-white">
        <thead class="table-light text-muted">
            <tr><th>CÓD</th><th>DOCENTE</th><th>ESPECIALIDAD</th></tr>
        </thead>
        <tbody>
            <?php foreach($profesores as $profe): ?>
            <tr>
                <td>COD-<?= $profe->id ?></td>
                <td class="fw-bold"><?= $profe->nombre ?></td>
                <td><span class="badge bg-primary bg-opacity-10 text-primary border border-primary-subtle px-3 py-2"><?= $profe->especialidad ?></span></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

5.4: El Formulario "Crear" Independiente

app/Views/profesores/crear.php
<div class="container d-flex justify-content-center mt-4">
    <div class="card shadow-sm border-0 p-5 w-50" style="border-radius:15px;">
        <h3 class="fw-bold mb-4 text-center text-dark">Reclutar Docente</h3>
        <form action="/demo/profesores/guardar" method="POST">
            <div class="form-floating mb-3 border border-primary rounded-3 overflow-hidden">
                <input type="text" name="nombre" class="form-control" required>
                <label>Nombre Completo</label>
            </div>
            <div class="form-floating mb-4 border rounded-3 overflow-hidden">
                <input type="text" name="espec" class="form-control bg-light" required>
                <label>Área (Ej: Física)</label>
            </div>
            <button type="submit" class="btn btn-primary w-100 fw-bold py-3 fs-5">GUARDAR</button>
        </form>
    </div>
</div>