View Class in Your Own MVC Framework
Now we will create the View class,
which will be responsible for data presentation.
It will take an object of the Page class as a parameter,
and return the ready HTML code of the page as its result,
which can then be output to the screen.
Let's see how we will use the View class
in the index.php file:
<?php
namespace Core;
error_reporting(E_ALL);
ini_set('display_errors', 'on');
spl_autoload_register(function($class) {
// your autoload implementation
});
$routes = require $_SERVER['DOCUMENT_ROOT'] . '/project/config/routes.php';
$track = ( new Router($routes) ) -> getTrack($_SERVER['REQUEST_URI']);
$page = ( new Dispatcher ) -> getPage($track);
echo (new View) -> render($page); // this is how we use the View class
?>
The code structure of the View class will have
the following form:
<?php
namespace Core;
class View
{
public function render(Page $page) {
return $this->renderLayout($page, $this->renderView($page));
}
private function renderLayout(Page $page, $content) {
}
private function renderView(Page $page) {
}
}
?>