From 040067c501661f83723b10a65e436bfdaa7c5d59 Mon Sep 17 00:00:00 2001 From: tp_dhu Date: Mon, 24 Mar 2025 01:17:43 +0000 Subject: [PATCH] first iteration of base controller --- app/controllers/BaseController.php | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 app/controllers/BaseController.php diff --git a/app/controllers/BaseController.php b/app/controllers/BaseController.php new file mode 100644 index 0000000..d417120 --- /dev/null +++ b/app/controllers/BaseController.php @@ -0,0 +1,58 @@ +f3 = \Base::instance(); + } + + + // helper function + + protected function getDB() + { + return $this->f3->get('DB'); + } + + /** + * Enforce that the user is logged in before proceeding. + */ + protected function requireLogin() + { + // using trait + $this->check_access($this->f3); + return; + + // abstract + if(!$this->f3->exists('SESSION.user')){ + $this->f3->set('SESSION.redirect', $this->f3->get('PATH')); + $this->f3->reroute('/login'); + } + } + + /** + * Set up a main layout template and inject the specified view path + * optional $data to pass variables down to template + */ + protected function renderView(string $viewPath, array $data = []):void + { + foreach($data as $key => $value){ + $this->f3->set($key, $value); + } + + // set {{content}} + $this->f3->set('content', $viewPath); + + // render tempalte + echo \Template::instance()->render('../ui/templates/layout.html'); + + // clear SESSION.error + $this->f3->clear('SESSION.error'); + } +} \ No newline at end of file