I'm writing a MVC app, I've put effort into writing index.php since it must be the entrance point (like main for C and Java) imho. I would like to ensure if someone who were to work on this file wouldn't be confused.
require_once 'App/config.php'; //main constants are defined here
require_once 'App/autoload.php';
if(strpos(URL,'error/'))
goto start;
try{
Connection::set(DBMS,HOST,PORT,DB_NAME,DB_USER,DB_PWD,$PDO_OPTIONS);
DAO::init();
}catch(Exception $e){
if(!PROD){ //if the app is online PROD=TRUE
throw $e;
}else
header('location: '.WEBROOT.'error/503/',503);
exit;
}
session_start();
if(!isset($_SESSION['user']))
$_SESSION["user"] = new Guest;
start:
try{
extract($_GET);
unset($_GET);
if(isset($controller, $action, $params))
Dispatcher::dispatch($controller, $action, $params);
else if (isset($controller, $action))
Dispatcher::dispatch($controller, $action);
else if (isset($controller, $params))
Dispatcher::dispatch($controller, NULL, $params);
else if (isset($controller))
Dispatcher::dispatch($controller);
else
Dispatcher::dispatch();
echo Dispatcher::deliver(); //output the response
}catch(Throwable $t){
if(!PROD)
throw $t;
else if($t instanceof TypeError && strpos($t->getTrace()[0],'Dispatcher.php'))
header('location: '.WEBROOT.'error/400/',400);
else if(strpos($t->getMessage(),'not found'))
header('location: '.WEBROOT.'error/404/',404);
else
header('location: '.WEBROOT.'error/503/',503);
}