Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file index.php
- <?php
- include_once "library/Autoloader.php";
- Autoloader::register();
- $application = new Application();
- $application->run();
- // file library/autoloader.php
- <?php
- class Autoloader
- {
- static function register()
- {
- spl_autoload_register(array(__CLASS__, 'autoload'));
- }
- static function autoload($name)
- {
- //todo: vérifier les fichiers autorisés
- $dir = "model";
- if (strpos($name, "Controller") !== FALSE)
- $dir = "controller";
- if (!file_exists($dir . "/" . $name . ".php")) {
- $dir = "library";
- }
- include_once $dir . "/" . $name . ".php";
- }
- }
- // file library/application.php
- <?php
- class Application
- {
- public function __construct()
- {
- $this->config();
- }
- public function run()
- {
- $this->route();
- }
- private function config()
- {
- error_reporting(E_ALL | E_STRICT);
- ini_set('display_errors', true);
- }
- public static function getParameters()
- {
- return array_merge($_GET, $_POST);
- }
- private function route()
- {
- $parameters = $this->getParameters();
- if (isset($parameters["controller"])) {
- $controller = ucfirst($parameters["controller"]) . "Controller";
- if (class_exists($controller)) {
- $c = new $controller();
- if (isset($parameters["action"])) {
- $action = strtolower($parameters["action"]);
- if (method_exists($c, $action)) {
- $c->$action();
- }
- } else {
- $c->index();
- }
- }
- } else {
- $c = new HomeController();
- $c->index();
- }
- }
- }
- // file library/database.php
- <?php
- class DataBase
- {
- private static $connexion;
- const DB_NAME = "recrutement-mvc";
- const DB_USER = "root";
- const DB_PASSWORD = "";
- private static function connect()
- {
- if (self::$connexion == null) {
- try {
- self::$connexion = new PDO("mysql:host=localhost;dbname=" . self::DB_NAME, self::DB_USER, self::DB_PASSWORD);
- self::$connexion->exec("SET CHARACTER SET utf8");
- } catch (Exception $e) {
- echo "Erreur de connexion à la base de données.";
- exit;
- }
- }
- }
- public static function getConnexion()
- {
- self::connect();
- return self::$connexion;
- }
- }
- // file controller/AbstractController.php
- <?php
- abstract class AbstractController
- {
- private $controller;
- private $model;
- private static $data;
- public function __construct()
- {
- $this->controller = get_class($this);
- $this->model = substr($this->controller, 0, strpos($this->controller, "Controller"));
- }
- public function render($view, $d = null)
- {
- self::$data = $d;
- $model = strtolower($this->model);
- if ($model == null) {
- $model = 'home';
- }
- if(file_exists("view/" . $model . "/" . $view . ".php")){
- include_once "view/header.php";
- include_once "view/" . $model . "/" . $view . ".php";
- include_once "view/footer.php";
- }
- }
- public function remove()
- {
- try {
- /** @var Model $r */
- $r = new $this->model(Application::getParameters()["id"]);
- $r->delete();
- } catch (Exception $e) {
- //(new HomeController())->render("index");
- // $this->render("error");
- }
- }
- public function getControllerName()
- {
- }
- public function getActionName()
- {
- }
- public function index()
- {
- $this->render("index");
- }
- public static function getData(){
- return self::$data;
- }
- }
- // file controller/UserController.php
- <?php
- class UserController extends AbstractController
- {
- public function index()
- {
- $this->render("index", User::findAll());
- }
- public function view()
- {
- try {
- $u = new User(Application::getParameters()["id"]);
- $this->render("view", $u);
- } catch (Exception $e) {
- (new HomeController())->render("index");
- }
- }
- public function add()
- {
- $this->render("add");
- }
- public function edit()
- {
- $this->render("edit");
- }
- }
- // file controller/RoleController.php
- <?php
- class RoleController extends AbstractController
- {
- public function index()
- {
- $this->render("index", Role::findAll());
- }
- public function view()
- {
- try {
- $r = new Role(Application::getParameters()["id"]);
- $this->render("view", $r);
- } catch (Exception $e) {
- (new HomeController())->render("index");
- }
- }
- public function add()
- {
- if (isset(Application::getParameters()["add_role"])) {
- if (!empty(Application::getParameters()["name"])) {
- $role = new Role();
- $role->name = Application::getParameters()["name"];
- $role->insert();
- header('Location: /?controller=role');
- }
- }
- $this->render("add");
- }
- public function edit()
- {
- $parameters = Application::getParameters();
- if (isset($parameters['id'])) {
- $id = $parameters['id'];
- try {
- $role = new Role($id);
- if (isset($parameters['edit_role'])) {
- if (!empty($parameters['name'])) {
- try {
- $role->name = $parameters['name'];
- header('Location: /?controller=role&action=index');
- } catch (Exception $e) {
- $this->render("edit", array('role' => $role, 'error' => 'Le role n\'a pas été mis à jour.'));
- }
- } else {
- $this->render("edit", array('role' => $role, 'error' => 'Le nom du rôle ne peut être vide.'));
- }
- }
- $this->render("edit", array('role' => $role));
- } catch (Exception $e) {
- (new HomeController())->render("index");
- }
- } else {
- (new HomeController())->render("index");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement