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

PHP App with Layered Archtiecture - Step 4

With this step we are going to create two new layers - application and views

Create folder views and core

  1. Folder views is for view files, it is empty now.
  2. Create folder core
  3. Create class file Application.php
  4. Add app.php file in core directory - now it is empty
blog
|_____config
|     |_____Database.php
|
|_____core
|     |_____Application.php
|
|_____service
|     |_____PostService.php 
|
|_____data
|     |_____Post.php
|
|_____views
|
|__________app.php
|
|__________index.php 							
						

Create autoloading for views

  1. Create class Application in folder core
  2. Add function for autoload of views
namespace core;

/**
 * Description of Application
 *
 * @author Evgenia
 */
class Application {
	 const VIEWS_FOLDER = 'views';

    public function load_view($templateName, $data = null)
    {
        include self::VIEWS_FOLDER
            . '/'
            . $templateName
            . '.php';
    }
}						
					

Moving autoload function in separated file

  1. The file app.php will make autoloading of classes
  2. Add session_start in app.php
  3. Add spl_autoload_registeindexr
  4. Include app.php at the beginning of index.php
session_start();
spl_autoload_register(function ($class_name) {
	include $class_name . '.php';
});
$db = new \config\Database();
$app = new \core\Application();						
					

This is index.php:

index