I am creating an MVC-Framework to learn more about how PHP and software architecture works in general. My goal is to create a simple website with it.
Right now I have trouble on what exactly is the job of the bootstrap file and the FrontController Class.
Can you have a look if what I did makes sense to you?
My Bootstrap-File looks like this:
<?php
use app\core\FrontController;
use app\core\Router;
// include configuration, database and autoloader
require_once(realpath(__DIR__ .DS.'..') . "/config/config.php");
require_once(realpath(__DIR__ .DS.'..') . "/config/db.php");
require_once(realpath(__DIR__ .DS.'..'.DS.'..') . "/vendor/autoloader.php"); // autoload for the core MVC Framework
// load composer autoloader
require_once(realpath(__DIR__ .DS.'..'.DS.'..') . "/vendor/autoload.php");
// set routes
$route = isset($_GET['route']) ? $_GET['route'] : null;
$action = isset($_GET['action']) ? $_GET['action'] : null;
// get FrontController
$fc = new FrontController(new Router, $route, $action);
echo $fc->output();
And the FrontController Class
<?php
namespace app\core;
class FrontController {
public $pdo;
private $route, $routeName, $model, $controller, $view, $twig;
public function __construct(Router $router, $routeName, $action = null) {
$this->pdo = new \PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
//Fetch a route based on a name, e.g. "search" or "list" or "edit"
$this->route = $router->getRoute($routeName);
$this->routeName = $routeName;
//Fetch the names of each component from the router
$modelName = "\app\model\\".$this->route->model;
$controllerName = "\app\controller\\".$this->route->controller;
$viewName = "\app\\view\\".$this->route->view;
//Instantiate each component
$this->model = new $modelName($this->pdo);
$this->controller = new $controllerName($this->model);
$this->view = new $viewName($this->model);
//Run the controller action
if(!empty($action) && method_exists($this->controller, $action)) $this->controller->{$action}();
// load TWIG
$loader = new \Twig_Loader_Filesystem(realpath(__DIR__ .DS.'..'.DS.'..') . "/site/themes/".TPL_DEFAULT."/templates/"); // *!* replace TPL_DEFAULT with $theme
$this->twig = new \Twig_Environment($loader, array(
'cache' => realpath(__DIR__ .DS.'..'.DS.'..') . "/cache/compilation/",
));
}
public function getRouteName() {
return $this->routeName;
}
public function getModel() {
return $this->model;
}
public function getController() {
return $this->controller;
}
public function getView() {
return $this->view;
}
public function output() {
var_dump($this->twig);
/* removed testing code for getting twig template on code Review SO */
$nav = $this->view->output($this->routeName, "nav");
var_dump($nav); // *!* test
$page = $this->view->output($this->routeName);
$title = $page["title"];
$content = $page["content"];
return $header . "<h1>" . $title . "</h1>" . $content . $footer;
}
}
I thought maybe you could review these two files and tell me if I understood the use of them correctly.
For me it's like bootstrap is there to just import stuff like config, autoloader, FrontController. And the FrontController does the basic handling of the Framework.
I hope this code makes at least some sense. You can find the whole project here btw.