소스 검색

added views and Site.php

Плотников Роман Вячеславович 3 년 전
부모
커밋
d0c26d1fbd
5개의 변경된 파일87개의 추가작업 그리고 6개의 파일을 삭제
  1. 8 5
      app/Controller/Site.php
  2. 60 0
      core/Src/View.php
  3. 1 1
      routes/web.php
  4. 17 0
      views/layouts/main.php
  5. 1 0
      views/site/hello.php

+ 8 - 5
app/Controller/Site.php

@@ -2,15 +2,18 @@
 
 namespace Controller;
 
+use Src\View;
+
 class Site
 {
-    public function index(): void
+    public function index(): string
     {
-        echo 'Index';
+        $view = new View();
+        return $view->render('site.hello', ['message' => 'index']);
     }
 
-    public function hello(): void
+    public function hello(): string
     {
-     echo 'Hello';
-    }
+        $view = new View();
+        return $view->render('site.hello', ['message' => 'hello']);    }
 }

+ 60 - 0
core/Src/View.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace Src;
+
+use Exception;
+
+class View
+{
+    private string $view = '';
+    private array $data = [];
+    private string $root = '';
+    private string $layout = 'layouts/main.php';
+
+    public function __construct(string $view = '', array $data = [])
+    {
+        $this->root = $this->getRoot();
+        $this->view = $view;
+        $this->data = $data;
+    }
+
+    private function getRoot():string
+    {
+        global $app;
+        $root = $app->settings->getRootPath();
+        $path = $app->settings->getViewsPath();
+        return $_SERVER['DOCUMENT_ROOT'] . $root . $path;
+    }
+
+    private function getPathToMain(): string
+    {
+        return $this->root . '/' . $this->layout;
+    }
+
+    private function getPathToView(string $view = ''): string
+    {
+        $view = str_replace('.', '/', $view);
+        return $this->getRoot() . "/$view.php";
+    }
+
+    public function render(string $view = '', array $data = []): string
+    {
+        $path = $this->getPathToView($view);
+
+        if(file_exists($this->getPathToMain()) && file_exists($path)) {
+            extract($data, EXTR_PREFIX_SAME, '');
+
+            ob_start();
+            require $path;
+
+            $content = ob_get_clean();
+            return require($this->getPathToMain());
+        }
+        throw new Exception('Render error');
+    }
+
+    public function __toString(): string
+    {
+        return $this->render($this->view, $this->data);
+    }
+}

+ 1 - 1
routes/web.php

@@ -2,5 +2,5 @@
 
 use Src\Route;
 
-Route::add('go', [Controller\Site::class, 'index']);
+Route::add('index', [Controller\Site::class, 'index']);
 Route::add('hello', [Controller\Site::class, 'hello']);

+ 17 - 0
views/layouts/main.php

@@ -0,0 +1,17 @@
+<!doctype html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport"
+          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
+    <meta http-equiv="X-UA-Compatible" content="ie=edge">
+    <title>Main site</title>
+</head>
+<body>
+
+<div>
+    <?= $content ?? ''; ?>
+</div>
+
+</body>
+</html>

+ 1 - 0
views/site/hello.php

@@ -0,0 +1 @@
+<h2><?= $message ?? ''; ?></h2>