Vorige week ben ik weer begonnen met het leren maken van een website d.m.v. MVC en daar is de volgende bootstrap uitgekomen:
<?php
// Class Bootstrap
class Bootstrap
{
// Initialize variables
private $_controller;
// Constructor
public function __construct()
{
// Explode url
$_url = explode('/', $_GET['url']);
// Check if url[0] is empty
if (empty($_url[0]))
{
// Require controller
require PATH_ROOT . '/controllers/index.php';
// Create controller
$this->_controller = new IndexController();
$this->_controller->index();
}
else
{
// Initialize variables
$_file = PATH_ROOT . '/controllers/' . $_url[0] . '.php';
$_class = $_url[0] . 'Controller';
$_arguments = array_slice($_url, 2);
// Check if file exists
if (file_exists($_file))
{
// Require controller
require $_file;
// Create controller
$this->_controller = new $_class;
$this->_controller->loadModel($_url[0]);
// Check if method exists
if (method_exists($this->_controller, $_url[1]))
{
// Execute method
$this->_controller->{$_url[1]}($_arguments);
}
else
{
// Execute method
$this->_controller->index($_arguments);
}
}
else
{
// Require controller
require PATH_ROOT . '/controllers/error.php';
// Create controller
$this->_controller = new ErrorController();
$this->_controller->index($_arguments);
}
}
}
}
?>
Het werkt zoals ik wil, maar ik heb geen idee of ik dit op een juiste manier heb gedaan, hebben jullie tips wat ik er beter aan kan?
Alvast bedankt!