/** * renders a view template using twig and passes needed data to the view * @param string $view | the view template file * @param array $data | array of parametres passed to the view */ public function view($view, $data = []) { /** * specifies the views directory for twig * @var object */ $loader = new \Twig_Loader_Filesystem(Config::get('paths.views')); /** * twig templating engine instance * @var object */ $twig = new \Twig_Environment($loader); /** * contains additional stuff needed to be present in the view * @var array */ $additionals = ['base' => Config::get('app.url'), 'home' => Config::get('app.url') . '/home']; /** * contains the result of mergin the passed data from the model & the additional stuff * @var array */ $passed_to_view = array_merge($data, $additionals); // render the template if it exists if (file_exists(Config::get('paths.views') . '/' . $view . '.html')) { echo $twig->render($view . '.html', $passed_to_view); } else { display_error('View file "' . $view . '" does not exist!'); } }
/** * establishes a database connection using PDO */ private function __construct() { try { // connect to the database $this->db = new PDO('mysql:host=' . Config::get('db.host') . ';dbname=' . Config::get('db.name'), Config::get('db.user'), Config::get('db.pass')); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // create tables if they don't exist $query = $this->db->exec("CREATE TABLE IF NOT EXISTS users(\n id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,\n username VARCHAR(25) NOT NULL,\n password VARCHAR(255) NOT NULL\n )"); $query = $this->db->exec("CREATE TABLE IF NOT EXISTS articles(\n id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,\n title VARCHAR(100) NOT NULL,\n body TEXT NOT NULL,\n created TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n )"); } catch (PDOException $e) { display_error($e->getMessage()); } }
<?php // enable error reporting error_reporting(E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); // start the session session_start(); // require vendor, autoload classes require_once '../vendor/autoload.php'; // set the configuration file \MVC\Helpers\Config::setConfigFile(dirname(__DIR__) . '/app/config/development.php');
/** * constructor */ public function __construct() { // set the session name $this->session_name = Config::get('session.name'); }