This commit is contained in:
2024-07-12 11:06:19 +03:00
12 changed files with 282 additions and 58 deletions

View File

@ -0,0 +1,14 @@
<?php
namespace app\controllers;
class Controller
{
protected \Twig\Loader\FilesystemLoader $loader;
protected \Twig\Environment $twig;
public function __construct()
{
$this->loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
$this->twig = new \Twig\Environment($this->loader, ['cache' => 'app/views/cache']);
}
}

View File

@ -3,10 +3,11 @@ namespace app\controllers;
use app\models\Question;
class QuestionController{
public function actionCreate(): void
class QuestionController extends Controller{
public function actionCreate()
{
require "app/views/questionCreate.php";
echo $this->twig->render('questionCreate.html');
}
public function actionGetQuestionsWithAnswers(): array

View File

@ -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 . "<br>";
$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();
}

14
app/views/mainLayout.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="ru-RU">
<head>
<meta charset="UTF-8">
<title>Примеры шаблонизатора Twig</title>
</head>
<body>
<h1>HEADER</h1>
{% block content %}
{% endblock %}
<h1>FOOTER</h1>
</body>
</html>

View File

@ -1,3 +1,6 @@
{% extends "mainLayout.html" %}
{% block content %}
<form action="/admin/question" method="post">
Вопрос: <br>
<label>
@ -6,4 +9,5 @@
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>
</form>
{% endblock %}

View File

@ -1,3 +1,6 @@
{% extends "mainLayout.html" %}
{% block content %}
<form action="/admin/user/" method="post">
Логин:<br>
<label>
@ -11,9 +14,10 @@
Email адрес: <br>
<label>
<input type="Email" name="email" required>
<input type="Email" name="email" required placeholder="Email">
</label> <br><br>
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>
</form>
{% endblock %}

26
app/views/userTable.html Normal file
View File

@ -0,0 +1,26 @@
{% extends "mainLayout.html" %}
{% block content %}
<table border="1" style="width: 80%;">
<thead>
<tr>
<td>id</td>
<td>Username</td>
<td>Email</td>
<td>Created at</td>
<td>Updated at</td>
</tr>
</thead>
<tbody>
{% for users in userArr %}
<tr>
<td>{{ users.id }}</td>
<td>{{ users.username }}</td>
<td>{{ users.email }}</td>
<td>{{ users.created_at }}</td>
<td>{{ users.updated_at }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

View File

@ -1,3 +1,6 @@
{% extends "mainLayout.html" %}
{% block content %}
<form action="/admin/user/edit" method="post">
id пользователя:<br>
<label>
@ -11,7 +14,7 @@
Пароль:<br>
<label>
<input type = "text" name = "password" required size="50" placeholder="Пароль">
<input type = "text" name = "password_hash" required size="50" placeholder="Пароль">
</label> <br> <br>
Email адрес: <br>
@ -21,4 +24,5 @@
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>
</form>
{% endblock %}