You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Web/Sources/src/shared/router/Router.php

88 lines
2.8 KiB

<?php
class Router{
private string $path = "/PHP/project/index.php";
private $router;
public function __construct()
{
global $dir,$vues;
try{
session_start();
$tabError = array();
$this->router = new AltoRouter();
$this->router->setBasePath($this->path);
$this->initialiseRoutes();
$match = $this->router->match();
if(!$match){
$tabError[] = 'error : wrong path ';
require($dir . $vues['error']);
}
switch ($match['target']){
case 'UserControler' :
$this->userConnexion('UserControler', $match);
break;
case 'VisteurControler' :
$this->callController('VisteurControler', $match);
break;
case 'any':
VisteurControler::displayView();
break;
default:
$tabError[] = 'error : Wrong call router';
require($dir . $vues['error']);
}
}catch (PDOException $exp){
$tabError[] = 'error data base' . $exp->getMessage();
require($dir . $vues['error']);
} catch (Exception $exp2){
$tabError[] = 'unknow exeption' . $exp2->getMessage();
require($dir . $vues['error']);
}catch (Error $e){
$tabError[] = 'unknow error' . $e->getMessage();
require($dir . $vues['error']);
}
}
private function userConnexion(string $controller, array $match) : void{
global $dir, $vues;
if(ModelUser::isUser() != NULL){
$controller = 'UserControler';
$this->callController($controller, $match);
}
else{
VisteurControler::displayView();
}
}
private function initialiseRoutes() : void{
$this->router->map( 'GET|POST', '/user/[a:action]?/[i:id]?', 'UserControler','user_action');
$this->router->map( 'GET|POST', '/', 'any');
$this->router->map( 'GET|POST', '/[a:action]/[i:id]?', 'VisteurControler','vistor_action');
}
private function callController(string $controller, array $match) : void{
global $dir, $vues;
$action = Validation::val_action($match['params']['action']);
$controller = new $controller;
if(isset($match['params']['id'])){
$param[] = $match['params']['id'];
} else
$param[] = array();
if(is_callable(array($controller, $action)))
call_user_func_array(array($controller, $action), $param);
else{
$tabError[] = 'error : controller ';
require($dir . $vues['error']);
}
}
}