Here we want to use function exctract(). Also we are adding service for the last post
namespace core;
class Application {
const VIEWS_FOLDER = 'views';
public function load_view($templateName, $data = null)
{
extract($data, EXTR_PREFIX_SAME, "wddx");
include self::VIEWS_FOLDER
. '/'
. $templateName
. '.php';
}
}
The index.php is not changed but index_view.php is now uncomplicated.
Just add the nessesary method to PostService
function readLastPost(){
$query = "SELECT * FROM post ORDER BY postID DESC LIMIT 1;";
$statement = $this->conn->prepare($query);
$data = new \data\Post();
if ($statement->execute()) {
foreach ($statement->fetchAll() as $row) {
$data->setId($row['postID']);
$data->setAuthor($row['author']);
$data->setCategory($row['category']);
$data->setTitle($row['title']);
$data->setContent($row['content']);
$data->setImg($row['img']);
}
}
return $data;
}
THis is index.php
//index.php
require_once 'app.php';
$list = new \service\ListService($db);
$s = new \service\PostService($db);
$c = $s->readLastPost();
$data['list']=$list;
$data['c']=$c;
$app->load_view('index_view', $data);
This is php code of index_view.php
<?php
//Categories and titles for links to any post
print_r($list->getCategories());
print_r($list->getTitles());
//The last post is here!
echo $c->getTitle().'<br>';
echo $c->getAuthor().'<br>' ;
echo $c->getContent().'<br>' ;
?>
Layer | Description |
---|---|
config | There are database link data |
core | There is an application class. It has function for loading views. |
service | Classes with SQL code for CRUD operation |
data | All classes in this layer corresponding to the fields in the database tables |
views | This folder contains HTML files of application's “front-end” |