ПОЛИТИКА ЗА ПОВЕРИТЕЛНОСТ И ЗАЩИТА НА ЛИЧНИ ДАННИ

PHP App with Layered Archtiecture - Step 6

Here we want to use function exctract(). Also we adding service for the last post

How to use function extract() in Application class?

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.

index

Find the last post!

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;
	}						
					

The scaffold of layered app is ready!

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>' ;
?>						
					

Conclusion

LayerDescription
configThere are database link data
coreThere is an application class. It has function for loading views.
serviceClasses with SQL code for CRUD operation
dataAll classes in this layer corresponding to the fields in the database tables
viewsThis folder contains HTML files of application's “front-end”