first iteration of base controller
This commit is contained in:
parent
2264882250
commit
040067c501
58
app/controllers/BaseController.php
Normal file
58
app/controllers/BaseController.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?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');
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user