-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
72 lines (61 loc) · 1.87 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace MVC;
class Router{
public $rutasGET=[];
public $rutasPOST=[];
public function Get($url, $fn){
$this->rutasGET[$url]= $fn;
}
public function Post($url, $fn){
$this->rutasPOST[$url]=$fn;
}
public function ComprobarRutas(){
session_start();
$aut=$_SESSION["login"] ?? null;
$rutas_protegidas=["/","/traveler","/paciente","/listarcitas","/servicios"];
$urlActual =$_SERVER['PATH_INFO']?? "/";
$metodo = $_SERVER['REQUEST_METHOD'];
if($metodo==='GET'){
$fn = $this->rutasGET[$urlActual] ?? null;
}
if($metodo==='POST'){
$fn = $this->rutasPOST[$urlActual] ?? null;
}
if(in_array($urlActual,$rutas_protegidas) && $aut !== true){
header("location: /login");
}
if($fn){
call_user_func($fn, $this);
}else{
echo "<h1>404</h1>";
echo "<p>Pagina no encontrada</P>";
}
}
public function Render($view, $datos=[]){
$usuario=$_SESSION["usuario"] ?? "";
foreach($datos as $key => $value){
$$key=$value;
}
ob_start();
include __DIR__."/view/$view.php";
$contenido= ob_get_clean();
include __DIR__."/view/main.php";
}
public function RenderAPI(string $json){
header("content-type:application/json");
echo $json;
}
public function RenderElement($element,$datos=[]){
foreach($datos as $key => $value){
$$key=$value;
}
ob_start();
include __DIR__."/view/$element.php";
}
public function RenderPague($view, $datos=[]){
foreach($datos as $key => $value){
$$key=$value;
}
include __DIR__."/view/$view.php";
}
}