58 lines
1.2 KiB
PHP
58 lines
1.2 KiB
PHP
<?php
|
|
|
|
abstract class BaseController
|
|
{
|
|
|
|
use RequiresAuth;
|
|
|
|
protected $f3;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->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');
|
|
}
|
|
} |