My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you what could be done better.
Some background: This application I'm building now is for use in ana school project with people that don't have not that much knowledge of PHP. Therefore I'm not using ana framework like Laravel, or implementing ana whole template engine, but I'm trying it to keep as simple as possible while still using classes and code separation.
The homeController:
Class homeController extends BaseController
{
public function index()
{
$this->view->make("common/home");
}
}
The View class:
Class View{
public static $file;
public function make($file){
View::$file = $file;
require_once DIR_VIEW . "default/template.php";
}
}
The template.php file:
<!DOCTYPE html>
<html>
<head>
<title>Just an title</title>
</head>
<body>
<?php require_once 'header.php'; ?>
<div id="mainContent">
<?php require_once DIR_VIEW . View::$file . ".php"; ?>
</div>
<?php require_once 'footer.php'; ?>
</body>
</html>
BaseController:
class BaseController
{
public $load;
public $url;
public $view;
public function __construct()
{
$this->load = new Loader();
$this->url = new Url();
$this->view = new View();
}
}
The files are required by an loader class with this piece of code: (don't know if it ads some relevance)
foreach (glob(DIR_LIB . "*.php") as $filename) {
require_once $filename;
}
The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but I know that that isn't the solution.
What do you think? Is this good enough or not even close?
Note: there will always be 1 file included, so that's why I thought that a simple require would succeed.