Ejemplo n.º 1
0
 /**
  * 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!');
     }
 }
Ejemplo n.º 2
0
 /**
  * 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());
     }
 }
Ejemplo n.º 3
0
 /**
  * constructor
  */
 public function __construct()
 {
     // set the session name
     $this->session_name = Config::get('session.name');
 }