Skip to content

Commit

Permalink
error and success messages
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-xz committed Jun 17, 2024
1 parent 74b0886 commit dd2744b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"slim/http": "^1.3",
"slim/php-view": "^3.2",
"php-di/php-di": "^7.0",
"nesbot/carbon": "^3.5"
"nesbot/carbon": "^3.5",
"slim/flash": "^0.4.0"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
Expand Down
54 changes: 53 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,34 @@

require __DIR__ . '/../vendor/autoload.php';

session_start();

$container = new Container();
$container->set('renderer', function () {
return new \Slim\Views\PhpRenderer(__DIR__ . '/../view');
});
$container->set('flash', function () {
return new \Slim\Flash\Messages();
});

$app = AppFactory::createFromContainer($container);
$app->addErrorMiddleware(true, true, true);
$app->add(MethodOverrideMiddleware::class);
$router = $app->getRouteCollector()->getRouteParser();

$app->get('/', function ($request, $response) {
$messages = $this->get('flash')->getMessages();
return $this->get('renderer')->render($response, 'index.phtml');
});

$app->get('/urls/{id}', function ($request, $response, $args) {
$messages = $this->get('flash')->getMessages();
$id = $args['id'];
$engine = new Engine('urls', 'find', $id);
$url = $engine->process();
$params = [
'url' => $url,
'flash' => $messages
];
return $this->get('renderer')->render($response, 'url.phtml', $params);
});
Expand All @@ -45,12 +53,15 @@
$validator = new Validator();
$url = $request->getParsedBodyParam('url');
$errors = $validator->validate($url);
if (count($errors) === 0) {
$engine = new Engine('urls', 'insert', $url['name']);
$insertedId = $engine->process();
$this->get('flash')->addMessage('success', 'Страница успешно добавлена');
return $response->withRedirect("/urls/{$insertedId}", 302);
}
$params = [
'url' => $url,
'errors' => $errors
'errors' => 5
];
return $this->get('renderer')->render($response, "index.phtml", $params);
});
Expand Down
1 change: 1 addition & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Validator
public function validate(array $url): array
{
$errors = [];
return [];
if (strlen($url['name']) > 255) {
$errors['strlen'] = "Url must be less than 256 characters";
} elseif (!filter_var($url['name'], FILTER_VALIDATE_URL)) {
Expand Down
9 changes: 8 additions & 1 deletion view/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@
<p class="fs-5 fw-light">Бесплатно проверяйте сайты на SEO пригодность</p>
<form action="/urls" class="row mb-3" method="post">
<div class="col-8">
<input type="text" name='url[name]' class="form-control form-control-lg" placeholder="https://www.example.com">
<?php if (!$errors == null) : ?>
<input type="text" name='url[name]' id="validationServer03" class="form-control is-invalid form-control-lg" placeholder="https://www.example.com" aria-describedby="validationServer03Feedback">
<div id="validationServer03Feedback" class="invalid-feedback">
Некорректный URL
</div>
<?php else : ?>
<input type="text" name='url[name]' id="validationServer03" class="form-control form-control-lg" placeholder="https://www.example.com" aria-describedby="validationServer03Feedback">
<?php endif ?>
</div>
<div class="col">
<input type="submit" class="btn btn-lg btn-primary ms-3 mx-3 px-5" value="Проверить">
Expand Down
11 changes: 10 additions & 1 deletion view/url.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@
</nav>
</header>
<main class="flex-grow-1">
<?php if (!$flash == null) : ?>
<?php foreach ($flash as $messages) : ?>
<?php foreach ($messages as $message) : ?>
<div class="alert alert-success" role="alert">
<?= htmlspecialchars($message)?>
</div>
<?php endforeach ?>
<?php endforeach?>
<?php endif ?>
<div class="container-lg">
<h1 class="mt-3">Сайт <?= htmlspecialchars($url['name'])?></h1>
<h1 class="mt-3">Сайт: <?= htmlspecialchars($url['name'])?></h1>
<div class="row">
<div class="col">
<table class="table table-bordered table-responsive text-nowrap table-hover">
Expand Down

0 comments on commit dd2744b

Please sign in to comment.