mirror of
https://github.com/Aviortheking/Blog_IMIE.git
synced 2025-07-24 03:49:51 +00:00
changement dans l'architecture du projet
This commit is contained in:
49
assets/php/Router.php
Normal file
49
assets/php/Router.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
/**
|
||||
* classe pour gerer le routage des pages
|
||||
* la variable static $router sert a utiliser le router dans plusieurs fichier
|
||||
* sans forcement avoir une obligation de nom de variable
|
||||
* de plus ils sert a garder une route unique
|
||||
*/
|
||||
class Router {
|
||||
//variable static pour stocker le router
|
||||
private static $router = null;
|
||||
|
||||
//definit le router
|
||||
public function __construct() {
|
||||
//TODO faire que si un router existe déjà retourner le routeur existant
|
||||
if(Router::$router != null) {
|
||||
return Router::$router;
|
||||
} else Router::$router = $this;
|
||||
}
|
||||
|
||||
//fonction static pour recuperer un router déjà crée
|
||||
public static function getRouter() {
|
||||
return Router::$router;
|
||||
}
|
||||
|
||||
//liste des routes
|
||||
private $routeList = array();
|
||||
|
||||
|
||||
//ajout d'une route
|
||||
public function addRoute($route, $page) {
|
||||
$this->routeList[$route] = $page;
|
||||
}
|
||||
|
||||
//fonction de recherche d'une route par rapport a un texte
|
||||
//return function
|
||||
public function search($path) {
|
||||
foreach ($this->routeList as $reg => $page) {
|
||||
if(preg_match($reg, $path)) {
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
return function () {
|
||||
return file_get_contents("../html/404.html");
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user