From ab0db2b320e7f37886675eac60a3a11319b1b062 Mon Sep 17 00:00:00 2001 From: stas Date: Thu, 11 Jul 2024 16:16:36 +0300 Subject: [PATCH] twig --- .gitignore | 3 +- app/controllers/Controller.php | 14 ++ app/controllers/QuestionController.php | 4 +- app/controllers/UserController.php | 22 +- app/views/mainLayout.html | 14 ++ ...questionCreate.php => questionCreate.html} | 6 +- app/views/{userCreate.php => userCreate.html} | 8 +- app/views/userTable.html | 26 +++ app/views/{userUpdate.php => userUpdate.html} | 8 +- composer.json | 3 +- composer.lock | 204 +++++++++++++++--- index.php | 25 ++- 12 files changed, 280 insertions(+), 57 deletions(-) create mode 100644 app/controllers/Controller.php create mode 100644 app/views/mainLayout.html rename app/views/{questionCreate.php => questionCreate.html} (80%) rename app/views/{userCreate.php => userCreate.html} (77%) create mode 100644 app/views/userTable.html rename app/views/{userUpdate.php => userUpdate.html} (78%) diff --git a/.gitignore b/.gitignore index 17560c2..94ad469 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea vendor -.env \ No newline at end of file +.env +cache \ No newline at end of file diff --git a/app/controllers/Controller.php b/app/controllers/Controller.php new file mode 100644 index 0000000..33c9fca --- /dev/null +++ b/app/controllers/Controller.php @@ -0,0 +1,14 @@ +loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views'); + $this->twig = new \Twig\Environment($this->loader, ['cache' => 'app/views/cache']); + } +} \ No newline at end of file diff --git a/app/controllers/QuestionController.php b/app/controllers/QuestionController.php index 348ab34..6903b1f 100644 --- a/app/controllers/QuestionController.php +++ b/app/controllers/QuestionController.php @@ -3,10 +3,10 @@ namespace app\controllers; use app\models\Question; -class QuestionController{ +class QuestionController extends Controller{ public function actionCreate() { - require "app/views/questionCreate.php"; + echo $this->twig->render('questionCreate.html'); } public function actionGetQuestionsWithAnswers() diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index d74832d..9358687 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -6,10 +6,12 @@ use app\helpers\Debug; use app\models\Question; use app\models\User; -class UserController { +class UserController extends Controller{ public function actionCreate(): void { - require "app/views/userCreate.php"; +// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views'); +// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']); + echo $this->twig->render('userCreate.html'); } public function actionAdd(): void @@ -25,10 +27,15 @@ class UserController { public function actionIndex(): void { +// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views'); +// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']); + + $i = 0; foreach (User::all() as $user) { - echo $user->username . "
"; + $userArr[$i++] = $user; } + echo $this->twig->render('userTable.html', ['userArr' => $userArr]); } public function actionView($id): void @@ -46,16 +53,19 @@ class UserController { public function actionUpdate(): void { - Debug::prn("Update"); - require "app/views/userUpdate.php"; +// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views'); +// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']); + echo $this->twig->render('userUpdate.html'); } public function actionEdit(): void { + $_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT); + $user = User::find($_REQUEST['id']); $user->username = $_REQUEST['username']; $user->email = $_REQUEST['email']; - $user->password = $_REQUEST['password']; + $user->password_hash = $_REQUEST['password_hash']; $user->save(); } diff --git a/app/views/mainLayout.html b/app/views/mainLayout.html new file mode 100644 index 0000000..d0f79f1 --- /dev/null +++ b/app/views/mainLayout.html @@ -0,0 +1,14 @@ + + + + + Примеры шаблонизатора Twig + + +

HEADER

+{% block content %} +{% endblock %} +

FOOTER

+ + + \ No newline at end of file diff --git a/app/views/questionCreate.php b/app/views/questionCreate.html similarity index 80% rename from app/views/questionCreate.php rename to app/views/questionCreate.html index bdcfd25..4c75022 100644 --- a/app/views/questionCreate.php +++ b/app/views/questionCreate.html @@ -1,3 +1,6 @@ +{% extends "mainLayout.html" %} + +{% block content %}
Вопрос: