7 13 Crud MVC PHP
7 13 Crud MVC PHP
Hemos trabajado toda la lógica en MVC. En la cual hemos separado por completo
el código HTML del lenguaje PHP implementando un controlador, modelo y vista.
FrontController (index.php)
Nuestro proyecto comienzan ejecutando el archivo index.php, que va a hacer el
papel de un FrontController, este es el encargado de saber que controlador y
acción se está ejecutando mientras que nuestro usuario navega. Nuestra lógica dice
que mediante la queryString habrán 2 parámetros esenciales para saber
que controlador y acción ejecutar el cual es "c" de controlador y "a" de acción.
<?php
require_once 'model/database.php';
SERVICIO NACIONAL DE APRENDIZAJE SENA
SISTEMA INTEGRADO DE GESTIÓN
Procedimiento Ejecución de la Formación Profesional Integral
MATERIAL DE APOYO GUÍA DE APRENDIZAJE
$controller = 'alumno';
if(!isset($_REQUEST['c']))
require_once "controller/$controller.controller.php";
$controller->Index();
else
$controller = strtolower($_REQUEST['c']);
// Instanciamos el controlador
require_once "controller/$controller.controller.php";
// Llama la accion
SERVICIO NACIONAL DE APRENDIZAJE SENA
SISTEMA INTEGRADO DE GESTIÓN
Procedimiento Ejecución de la Formación Profesional Integral
MATERIAL DE APOYO GUÍA DE APRENDIZAJE
Controlador
Ahora debemos crear nuestras acciones que en nuestro caso
mostrara diferentes vistas o acciones a realizar como guardar/insertar/eliminar.
<?php
require_once 'model/alumno.php';
class AlumnoController{
private $model;
require_once 'view/header.php';
require_once 'view/alumno/alumno.php';
require_once 'view/footer.php';
}
SERVICIO NACIONAL DE APRENDIZAJE SENA
SISTEMA INTEGRADO DE GESTIÓN
Procedimiento Ejecución de la Formación Profesional Integral
MATERIAL DE APOYO GUÍA DE APRENDIZAJE
if(isset($_REQUEST['id'])){
$alm = $this->model->Obtener($_REQUEST['id']);
require_once 'view/header.php';
require_once 'view/alumno/alumno-editar.php';
require_once 'view/footer.php';
$alm->id = $_REQUEST['id'];
$alm->Nombre = $_REQUEST['Nombre'];
$alm->Apellido = $_REQUEST['Apellido'];
$alm->Correo = $_REQUEST['Correo'];
$alm->Sexo = $_REQUEST['Sexo'];
$alm->FechaNacimiento = $_REQUEST['FechaNacimiento'];
$alm->id > 0
SERVICIO NACIONAL DE APRENDIZAJE SENA
SISTEMA INTEGRADO DE GESTIÓN
Procedimiento Ejecución de la Formación Profesional Integral
MATERIAL DE APOYO GUÍA DE APRENDIZAJE
? $this->model->Actualizar($alm)
: $this->model->Registrar($alm);
header('Location: index.php');
$this->model->Eliminar($_REQUEST['id']);
header('Location: index.php');
Vista
Esta contendrá el código HTML junto a algo de PHP para mostrarle al usuario
sus interfaces de navegación. En nuestro caso hemos creado 2 archivos, uno
llamado alumno.php que usaremos para listar la data en una tabla html y alumno-
editar.php para crear/actualizar alumnos.
<h1 class="page-header">Alumnos</h1>
</div>
<thead>
<tr>
<th >Nombre</th>
<th>Apellido</th>
<th>Correo</th>
<th >Sexo</th>
<th >Nacimiento</th>
<th ></th>
<th ></th>
</tr>
</thead>
<tbody>
<tr>
<td>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<h1 class="page-header">
</h1>
<ol class="breadcrumb">
<li><a href="?c=Alumno">Alumnos</a></li>
</ol>
<div class="form-group">
<label>Nombre</label>
</div>
<div class="form-group">
<label>Apellido</label>
</div>
<div class="form-group">
SERVICIO NACIONAL DE APRENDIZAJE SENA
SISTEMA INTEGRADO DE GESTIÓN
Procedimiento Ejecución de la Formación Profesional Integral
MATERIAL DE APOYO GUÍA DE APRENDIZAJE
<label>Correo</label>
</div>
<div class="form-group">
<label>Sexo</label>
</select>
</div>
<div class="form-group">
<label>Fecha de nacimiento</label>
</div>
<hr />
<div class="text-right">
SERVICIO NACIONAL DE APRENDIZAJE SENA
SISTEMA INTEGRADO DE GESTIÓN
Procedimiento Ejecución de la Formación Profesional Integral
MATERIAL DE APOYO GUÍA DE APRENDIZAJE
</div>
</form>
<script>
$(document).ready(function(){
$("#frm-alumno").submit(function(){
return $(this).validate();
});
})
</script>
require_once 'view/header.php';
require_once 'view/alumno/alumno.php';
require_once 'view/footer.php';
Con esto evitamos tener que duplicar código haciendo nuestro aplicativo más
escalable. Y Así finalizamos el montaje de este ejemplo.