Final Code of the View Class in Your Own MVC Framework
Let's put all our code together:
<?php
namespace Core;
class View
{
public function render(Page $page) {
return $this->renderLayout($page, $this->renderView($page));
}
private function renderLayout(Page $page, $content) {
$layoutPath = $_SERVER['DOCUMENT_ROOT'] . "/project/layouts/{$page->layout}.php";
if (file_exists($layoutPath)) {
ob_start();
$title = $page->title;
include $layoutPath;
return ob_get_clean();
}
}
private function renderView(Page $page) {
$viewPath = $_SERVER['DOCUMENT_ROOT'] . "/project/views/{$page->view}.php";
if (file_exists($viewPath)) {
ob_start();
$data = $page->data;
extract($data);
include $viewPath;
return ob_get_clean();
}
}
}
?>
Analyze the provided code of the View class.
Then, on your own, without looking at my
code, implement the same class. Check
its operation.