This is how I manage content on my site:
PageLoader.class
class PageLoader {
private $page_dir;
private $page_headers = '';
private $page_html = '';
public function __construct($page_dir)
{
$this->page_dir = $page_dir;
}
public function load()
{
$file_found = false;
ob_start();
$file_found = include("./{$this->page_dir}");
$file_contents = ob_get_contents();
ob_end_clean();
if($file_found != false)
{
$this->page_html = $file_contents;
}
}
public function outputBody()
{
echo $this->page_html;
}
}
index.php
$connection = mysql_connect(....);
$is_user_logged = login(...);
$view = new PageLoader($_GET['page']);
$view->load();
?>
<html>
<head>
<? $view->outputHeaders(); ?>
</head>
<body>
<? $view->outputBody(); ?>
</body>
</html>
One problem with this:
Those two variables $connection and $is_user_logged_in are not accessible from within load() method. Most of my inner pages depend on those variables for various reasons. Since they both appear NULL in that scope, inner pages fail to function.
This could solve the problem: $view->setVariable("connection", $connection) but I have a lot more than 2 'main' variables so I'm not sure if this is the best way...
What can I do? Feel free to suggest any alternative ways for me to manage my content as my way is probably the least professional...