Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. // file index.php
  2.  
  3. <?php
  4.  
  5. include_once "library/Autoloader.php";
  6. Autoloader::register();
  7.  
  8. $application = new Application();
  9. $application->run();
  10.  
  11.  
  12. // file library/autoloader.php
  13.  
  14. <?php
  15.  
  16.  
  17. class Autoloader
  18. {
  19.  
  20. static function register()
  21. {
  22. spl_autoload_register(array(__CLASS__, 'autoload'));
  23. }
  24.  
  25. static function autoload($name)
  26. {
  27. //todo: vérifier les fichiers autorisés
  28. $dir = "model";
  29. if (strpos($name, "Controller") !== FALSE)
  30. $dir = "controller";
  31.  
  32. if (!file_exists($dir . "/" . $name . ".php")) {
  33. $dir = "library";
  34. }
  35. include_once $dir . "/" . $name . ".php";
  36. }
  37.  
  38. }
  39.  
  40.  
  41. // file library/application.php
  42.  
  43. <?php
  44.  
  45. class Application
  46. {
  47. public function __construct()
  48. {
  49. $this->config();
  50. }
  51.  
  52. public function run()
  53. {
  54. $this->route();
  55. }
  56.  
  57. private function config()
  58. {
  59. error_reporting(E_ALL | E_STRICT);
  60. ini_set('display_errors', true);
  61. }
  62.  
  63. public static function getParameters()
  64. {
  65. return array_merge($_GET, $_POST);
  66. }
  67.  
  68. private function route()
  69. {
  70. $parameters = $this->getParameters();
  71.  
  72. if (isset($parameters["controller"])) {
  73. $controller = ucfirst($parameters["controller"]) . "Controller";
  74.  
  75. if (class_exists($controller)) {
  76. $c = new $controller();
  77.  
  78. if (isset($parameters["action"])) {
  79. $action = strtolower($parameters["action"]);
  80.  
  81. if (method_exists($c, $action)) {
  82. $c->$action();
  83. }
  84.  
  85. } else {
  86. $c->index();
  87. }
  88. }
  89.  
  90. } else {
  91. $c = new HomeController();
  92. $c->index();
  93. }
  94. }
  95. }
  96.  
  97. // file library/database.php
  98.  
  99. <?php
  100.  
  101. class DataBase
  102. {
  103. private static $connexion;
  104.  
  105. const DB_NAME = "recrutement-mvc";
  106. const DB_USER = "root";
  107. const DB_PASSWORD = "";
  108.  
  109. private static function connect()
  110. {
  111. if (self::$connexion == null) {
  112. try {
  113. self::$connexion = new PDO("mysql:host=localhost;dbname=" . self::DB_NAME, self::DB_USER, self::DB_PASSWORD);
  114. self::$connexion->exec("SET CHARACTER SET utf8");
  115. } catch (Exception $e) {
  116. echo "Erreur de connexion à la base de données.";
  117. exit;
  118. }
  119. }
  120. }
  121.  
  122. public static function getConnexion()
  123. {
  124. self::connect();
  125. return self::$connexion;
  126. }
  127.  
  128. }
  129.  
  130. // file controller/AbstractController.php
  131.  
  132. <?php
  133.  
  134. abstract class AbstractController
  135. {
  136. private $controller;
  137. private $model;
  138. private static $data;
  139.  
  140.  
  141. public function __construct()
  142. {
  143. $this->controller = get_class($this);
  144. $this->model = substr($this->controller, 0, strpos($this->controller, "Controller"));
  145. }
  146.  
  147. public function render($view, $d = null)
  148. {
  149. self::$data = $d;
  150.  
  151. $model = strtolower($this->model);
  152. if ($model == null) {
  153. $model = 'home';
  154. }
  155.  
  156. if(file_exists("view/" . $model . "/" . $view . ".php")){
  157. include_once "view/header.php";
  158. include_once "view/" . $model . "/" . $view . ".php";
  159. include_once "view/footer.php";
  160. }
  161.  
  162. }
  163.  
  164. public function remove()
  165. {
  166. try {
  167. /** @var Model $r */
  168. $r = new $this->model(Application::getParameters()["id"]);
  169. $r->delete();
  170.  
  171. } catch (Exception $e) {
  172. //(new HomeController())->render("index");
  173. // $this->render("error");
  174. }
  175. }
  176.  
  177. public function getControllerName()
  178. {
  179. }
  180.  
  181. public function getActionName()
  182. {
  183.  
  184. }
  185.  
  186. public function index()
  187. {
  188. $this->render("index");
  189. }
  190.  
  191. public static function getData(){
  192. return self::$data;
  193. }
  194. }
  195.  
  196. // file controller/UserController.php
  197.  
  198. <?php
  199.  
  200. class UserController extends AbstractController
  201. {
  202.  
  203. public function index()
  204. {
  205. $this->render("index", User::findAll());
  206. }
  207.  
  208. public function view()
  209. {
  210. try {
  211. $u = new User(Application::getParameters()["id"]);
  212.  
  213. $this->render("view", $u);
  214. } catch (Exception $e) {
  215. (new HomeController())->render("index");
  216. }
  217. }
  218.  
  219. public function add()
  220. {
  221. $this->render("add");
  222. }
  223.  
  224. public function edit()
  225. {
  226. $this->render("edit");
  227. }
  228.  
  229.  
  230. }
  231.  
  232.  
  233. // file controller/RoleController.php
  234.  
  235. <?php
  236.  
  237. class RoleController extends AbstractController
  238. {
  239. public function index()
  240. {
  241. $this->render("index", Role::findAll());
  242. }
  243.  
  244. public function view()
  245. {
  246. try {
  247. $r = new Role(Application::getParameters()["id"]);
  248. $this->render("view", $r);
  249. } catch (Exception $e) {
  250. (new HomeController())->render("index");
  251. }
  252. }
  253.  
  254. public function add()
  255. {
  256. if (isset(Application::getParameters()["add_role"])) {
  257. if (!empty(Application::getParameters()["name"])) {
  258. $role = new Role();
  259. $role->name = Application::getParameters()["name"];
  260.  
  261. $role->insert();
  262.  
  263. header('Location: /?controller=role');
  264. }
  265. }
  266.  
  267. $this->render("add");
  268. }
  269.  
  270. public function edit()
  271. {
  272. $parameters = Application::getParameters();
  273.  
  274. if (isset($parameters['id'])) {
  275. $id = $parameters['id'];
  276.  
  277. try {
  278. $role = new Role($id);
  279.  
  280. if (isset($parameters['edit_role'])) {
  281. if (!empty($parameters['name'])) {
  282.  
  283. try {
  284. $role->name = $parameters['name'];
  285. header('Location: /?controller=role&action=index');
  286.  
  287. } catch (Exception $e) {
  288. $this->render("edit", array('role' => $role, 'error' => 'Le role n\'a pas été mis à jour.'));
  289. }
  290.  
  291. } else {
  292. $this->render("edit", array('role' => $role, 'error' => 'Le nom du rôle ne peut être vide.'));
  293. }
  294. }
  295.  
  296. $this->render("edit", array('role' => $role));
  297.  
  298. } catch (Exception $e) {
  299. (new HomeController())->render("index");
  300. }
  301. } else {
  302. (new HomeController())->render("index");
  303. }
  304. }
  305.  
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement