This commit is contained in:
Билай Станислав 2024-07-11 16:16:36 +03:00
parent 3df81597b6
commit ab0db2b320
12 changed files with 280 additions and 57 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
vendor
.env
cache

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,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()

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>
@ -7,3 +10,4 @@
<input type = "submit" value="Подтвердить">
<input type="reset">
</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>
{% 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>
@ -22,3 +25,4 @@
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>
{% endblock %}

View File

@ -5,7 +5,8 @@
"require": {
"illuminate/database": "^11.14",
"craft-group/phroute": "^2.1",
"vlucas/phpdotenv": "^5.6"
"vlucas/phpdotenv": "^5.6",
"twig/twig": "^3.0"
},
"autoload": {
"psr-4": {

204
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "0f18467a4a4a8e17b3a359c92f94a062",
"content-hash": "ec1e05ff0752e8d6ad1df75a5cae6188",
"packages": [
{
"name": "brick/math",
@ -337,16 +337,16 @@
},
{
"name": "illuminate/collections",
"version": "v11.14.0",
"version": "v11.15.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/collections.git",
"reference": "358fd6dcce6927ee9d7cf520fa619f4597020d52"
"reference": "9bf68f03bbe05d38c9bd99bac8798c0de61f8478"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/collections/zipball/358fd6dcce6927ee9d7cf520fa619f4597020d52",
"reference": "358fd6dcce6927ee9d7cf520fa619f4597020d52",
"url": "https://api.github.com/repos/illuminate/collections/zipball/9bf68f03bbe05d38c9bd99bac8798c0de61f8478",
"reference": "9bf68f03bbe05d38c9bd99bac8798c0de61f8478",
"shasum": ""
},
"require": {
@ -388,11 +388,11 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2024-06-28T20:14:10+00:00"
"time": "2024-07-02T20:54:37+00:00"
},
{
"name": "illuminate/conditionable",
"version": "v11.14.0",
"version": "v11.15.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/conditionable.git",
@ -438,16 +438,16 @@
},
{
"name": "illuminate/container",
"version": "v11.14.0",
"version": "v11.15.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/container.git",
"reference": "280405e0b577504b97feae0e7c7f5399816ccff1"
"reference": "49183db6643a7efbe9902ca379b8f8a55c802f88"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/container/zipball/280405e0b577504b97feae0e7c7f5399816ccff1",
"reference": "280405e0b577504b97feae0e7c7f5399816ccff1",
"url": "https://api.github.com/repos/illuminate/container/zipball/49183db6643a7efbe9902ca379b8f8a55c802f88",
"reference": "49183db6643a7efbe9902ca379b8f8a55c802f88",
"shasum": ""
},
"require": {
@ -485,20 +485,20 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2024-07-02T15:41:17+00:00"
"time": "2024-07-03T21:04:00+00:00"
},
{
"name": "illuminate/contracts",
"version": "v11.14.0",
"version": "v11.15.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/contracts.git",
"reference": "eb8ccfbc5905c5631712d157cccdd7bc9db692e0"
"reference": "be935e9d9115a57be74d20176f43fa8a207029f3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/contracts/zipball/eb8ccfbc5905c5631712d157cccdd7bc9db692e0",
"reference": "eb8ccfbc5905c5631712d157cccdd7bc9db692e0",
"url": "https://api.github.com/repos/illuminate/contracts/zipball/be935e9d9115a57be74d20176f43fa8a207029f3",
"reference": "be935e9d9115a57be74d20176f43fa8a207029f3",
"shasum": ""
},
"require": {
@ -533,20 +533,20 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2024-07-01T21:58:24+00:00"
"time": "2024-07-09T13:57:38+00:00"
},
{
"name": "illuminate/database",
"version": "v11.14.0",
"version": "v11.15.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/database.git",
"reference": "6576f6fcc871a6ad173b6e246e8d2c63cd7cfe1a"
"reference": "9b7b13d2d9175ae9c07d4744d8f7d5b07e7264a7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/database/zipball/6576f6fcc871a6ad173b6e246e8d2c63cd7cfe1a",
"reference": "6576f6fcc871a6ad173b6e246e8d2c63cd7cfe1a",
"url": "https://api.github.com/repos/illuminate/database/zipball/9b7b13d2d9175ae9c07d4744d8f7d5b07e7264a7",
"reference": "9b7b13d2d9175ae9c07d4744d8f7d5b07e7264a7",
"shasum": ""
},
"require": {
@ -601,11 +601,11 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2024-07-02T15:56:54+00:00"
"time": "2024-07-08T15:08:27+00:00"
},
{
"name": "illuminate/macroable",
"version": "v11.14.0",
"version": "v11.15.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/macroable.git",
@ -651,16 +651,16 @@
},
{
"name": "illuminate/support",
"version": "v11.14.0",
"version": "v11.15.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/support.git",
"reference": "a8f299ed76d52fc048decada3571628e65fa5c41"
"reference": "b551f6cbecc607bcaf520eb3fc1933db9b1398f2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/support/zipball/a8f299ed76d52fc048decada3571628e65fa5c41",
"reference": "a8f299ed76d52fc048decada3571628e65fa5c41",
"url": "https://api.github.com/repos/illuminate/support/zipball/b551f6cbecc607bcaf520eb3fc1933db9b1398f2",
"reference": "b551f6cbecc607bcaf520eb3fc1933db9b1398f2",
"shasum": ""
},
"require": {
@ -721,7 +721,7 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2024-07-01T21:58:57+00:00"
"time": "2024-07-08T14:46:56+00:00"
},
{
"name": "nesbot/carbon",
@ -1130,6 +1130,73 @@
],
"time": "2024-05-31T14:57:53+00:00"
},
{
"name": "symfony/deprecation-contracts",
"version": "v3.5.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/deprecation-contracts.git",
"reference": "0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1",
"reference": "0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1",
"shasum": ""
},
"require": {
"php": ">=8.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "3.5-dev"
},
"thanks": {
"name": "symfony/contracts",
"url": "https://github.com/symfony/contracts"
}
},
"autoload": {
"files": [
"function.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "A generic function and convention to trigger deprecation notices",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/deprecation-contracts/tree/v3.5.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-04-18T09:32:20+00:00"
},
{
"name": "symfony/polyfill-ctype",
"version": "v1.30.0",
@ -1617,6 +1684,85 @@
],
"time": "2024-04-18T09:32:20+00:00"
},
{
"name": "twig/twig",
"version": "v3.10.3",
"source": {
"type": "git",
"url": "https://github.com/twigphp/Twig.git",
"reference": "67f29781ffafa520b0bbfbd8384674b42db04572"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/67f29781ffafa520b0bbfbd8384674b42db04572",
"reference": "67f29781ffafa520b0bbfbd8384674b42db04572",
"shasum": ""
},
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/polyfill-ctype": "^1.8",
"symfony/polyfill-mbstring": "^1.3",
"symfony/polyfill-php80": "^1.22"
},
"require-dev": {
"psr/container": "^1.0|^2.0",
"symfony/phpunit-bridge": "^5.4.9|^6.4|^7.0"
},
"type": "library",
"autoload": {
"files": [
"src/Resources/core.php",
"src/Resources/debug.php",
"src/Resources/escaper.php",
"src/Resources/string_loader.php"
],
"psr-4": {
"Twig\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
},
{
"name": "Twig Team",
"role": "Contributors"
},
{
"name": "Armin Ronacher",
"email": "armin.ronacher@active-4.com",
"role": "Project Founder"
}
],
"description": "Twig, the flexible, fast, and secure template language for PHP",
"homepage": "https://twig.symfony.com",
"keywords": [
"templating"
],
"support": {
"issues": "https://github.com/twigphp/Twig/issues",
"source": "https://github.com/twigphp/Twig/tree/v3.10.3"
},
"funding": [
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/twig/twig",
"type": "tidelift"
}
],
"time": "2024-05-16T10:04:27+00:00"
},
{
"name": "vlucas/phpdotenv",
"version": "v5.6.0",
@ -1784,5 +1930,5 @@
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.3.0"
"plugin-api-version": "2.6.0"
}

View File

@ -14,7 +14,6 @@ use app\controllers\PostController;
use app\helpers\Debug;
use Phroute\Phroute\RouteCollector;
$router = new RouteCollector();
$router->get('/', [MainController::class, 'actionIndex']);
$router->get('/example', [MainController::class, 'actionExample']);
@ -28,18 +27,18 @@ $router->group(["prefix" => "admin"], function (RouteCollector $router){
$router->post("/", [\app\controllers\UserController::class, 'actionAdd']);
$router->post("/edit", [\app\controllers\UserController::class, 'actionEdit']);
});
$router->group(["prefix" => "question"], function (RouteCollector $router){
$router->get('/create', [QuestionController::class, 'actionCreate']);
$router->get('/update', [QuestionController::class, 'actionUpdate']);
$router->get('/delete/{id}', [QuestionController::class, 'actionDelete']);
$router->get('/', [QuestionController::class, 'actionIndex']);
$router->get('/{id}', [QuestionController::class, 'actionView']);
$router->post("/", [QuestionController::class, 'actionAdd']);
$router->post("/edit", [QuestionController::class, 'actionEdit']);
});
$router->group(["prefix" => "post"], function (RouteCollector $router){
$router->get('/', [\app\controllers\PostController::class, 'actionIndex']);
});
// $router->group(["prefix" => "question"], function (RouteCollector $router){
// $router->get('/create', [QuestionController::class, 'actionCreate']);
// $router->get('/update', [QuestionController::class, 'actionUpdate']);
// $router->get('/delete/{id}', [QuestionController::class, 'actionDelete']);
// $router->get('/', [QuestionController::class, 'actionIndex']);
// $router->get('/{id}', [QuestionController::class, 'actionView']);
// $router->post("/", [QuestionController::class, 'actionAdd']);
// $router->post("/edit", [QuestionController::class, 'actionEdit']);
// });
// $router->group(["prefix" => "post"], function (RouteCollector $router){
// $router->get('/', [\app\controllers\PostController::class, 'actionIndex']);
// });
});
$router->get('/allQuestions', [QuestionController::class, 'actionViewAllQuestions']);