Skip to content

Commit

Permalink
upgraded container, so flash messages are called constantly, urls are…
Browse files Browse the repository at this point in the history
… passed threw router
  • Loading branch information
vladimir-xz committed Jul 1, 2024
1 parent 6879d27 commit 094e73c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 65 deletions.
File renamed without changes.
35 changes: 23 additions & 12 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
session_start();

$container = new Container();
$container->set('renderer', function () {
$phpView = new \Slim\Views\PhpRenderer(__DIR__ . '/../view');
$phpView->setLayout('layout.phtml');
return $phpView;
});

$container->set('flash', function () {
return new \Slim\Flash\Messages();
});
Expand All @@ -26,22 +22,37 @@
$app->add(MethodOverrideMiddleware::class);
$router = $app->getRouteCollector()->getRouteParser();

$container->set('renderer', function () use ($router, $container) {
$messages = $container->get('flash')->getMessages();
$phpView = new \Slim\Views\PhpRenderer(
__DIR__ . '/../view',
[
'flash' => $messages,
'index' => $router->urlFor('index'),
'urlsShow' => fn($id) => $router->urlFor('urls.show', ['id' => $id]),
'urlsIndex' => $router->urlFor('urls.index'),
'urlsStore' => $router->urlFor('urls.store'),
'urlsCheck' => fn($id) => $router->urlFor('urls.checks', ['url_id' => $id])
]
);
$phpView->setLayout('layout.phtml');
return $phpView;
});

$app->get('/', function ($request, $response) {
return $this->get('renderer')->render($response, 'index.phtml', ['main' => 'active']);
})->setName('index');

$app->get('/urls/{id}', function ($request, $response, $args) {
$messages = $this->get('flash')->getMessages();
$app->get('/urls/{id:[0-9]+}', function ($request, $response, $args) {
$id = $args['id'];
$dbHandler = new DbHandler('urls');
if (!is_numeric($id) || !$url = $dbHandler->process('find by id', $id)) {
if (!$url = $dbHandler->process('find by id', $id)) {
return $response->withStatus(404);
}
$checkRecords = $dbHandler->process('get check records', $id);
$params = [
'url' => $url,
'checks' => $checkRecords,
'flash' => $messages
];
return $this->get('renderer')->render($response, '/urls/show.phtml', $params);
})->setName('urls.show');
Expand Down Expand Up @@ -82,11 +93,11 @@
}
})->setName('urls.store');

$app->post('/urls/{url_id}/checks', function ($request, $response, $args) use ($router) {
$app->post('/urls/{url_id:[0-9]+}/checks', function ($request, $response, $args) use ($router) {
$dbHandler = new DbHandler('urls');
$analyzer = new Engine('Check Connection', 'Check Params');
$id = $args['url_id'];
if (!is_numeric($id) || !$url = $dbHandler->process('find by id', $id)) {
if (!$url = $dbHandler->process('find by id', $id)) {
return $response->withStatus(404);
}
$checkResult = $analyzer->process($url);
Expand All @@ -100,7 +111,7 @@
$this->get('flash')->addMessage('danger', 'Произошла ошибка при проверке, не удалось подключиться');
}
return $response->withRedirect($router->
urlFor('urls.show', ['id' => strval($id)]), 303);
urlFor('urls.show', ['id' => $id]), 303);
})->setName('urls.checks');

$app->run();
42 changes: 18 additions & 24 deletions view/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,25 @@
<div class="col-12 col-md-10 col-lg-8 border bg-light mx-auto rounded-3 p-5">
<h1 class="display-3">Анализатор страниц</h1>
<p class="fs-5 fw-light">Бесплатно проверяйте сайты на SEO пригодность</p>
<form action="/urls" class="row mb-3" method="post">
<form action="<?= $urlsStore?>" class="row mb-3" method="post">
<div class="col-8">
<?php if (isset($error)) : ?>
<input
type="text"
name='url[name]'
id="validationServer03"
class="form-control is-invalid form-control-lg"
value="<?= htmlspecialchars($url ?? '') ?>"
placeholder="https://www.example.com"
aria-describedby="validationServer03Feedback"
>
<div id="validationServer03Feedback" class="invalid-feedback">
<?= $error?>
</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 ?>
<input
type="text"
name='url[name]'
id="validationServer03"
class="
form-control
<?php if (isset($error)) :?>
<?= 'is-invalid'?>
<?php endif ?>
form-control-lg"
value="<?= htmlspecialchars($url ?? '') ?>"
placeholder="https://www.example.com"
aria-describedby="validationServer03Feedback"
>
<div id="validationServer03Feedback" class="invalid-feedback">
<?= $error?>
</div>
</div>
<div class="col-2">
<input
Expand Down
6 changes: 3 additions & 3 deletions view/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<header class="flex-shrink-0">
<nav class="navbar navbar-expand-md navbar-dark bg-dark px-3" data-bs-theme="dark">
<div class="container-fluid">
<a class="navbar-brand" href="/">Анализатор страниц</a>
<a class="navbar-brand" href="<?= $index ?>">Анализатор страниц</a>
<button
class="navbar-toggler"
type="button"
Expand All @@ -27,10 +27,10 @@
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link <?= $main ?? '' ?>" aria-current="page" href="/">Главная</a>
<a class="nav-link <?= $main ?? '' ?>" aria-current="page" href="<?= $index ?>">Главная</a>
</li>
<li class="nav-item">
<a class="nav-link <?= $pages ?? '' ?>" href="/urls">Сайты</a>
<a class="nav-link <?= $pages ?? '' ?>" href="<?= $urlsIndex?>">Сайты</a>
</li>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion view/urls/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<tr>
<td><?= htmlspecialchars($url->id)?></td>
<td>
<a href="/urls/<?= htmlspecialchars($url->id)?>">
<a href="<?= $urlsShow(htmlspecialchars($url->id))?>">
<?= htmlspecialchars($url->name)?>
</a>
</td>
Expand Down
48 changes: 23 additions & 25 deletions view/urls/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</div>
</div>
<h2 class="mt-5">Проверки</h2>
<form action="/urls/<?= htmlspecialchars($url->id)?>/checks" class="my-3" method="post">
<form action="<?= $urlsCheck(htmlspecialchars($url->id))?>" class="my-3" method="post">
<input type="submit" class="btn btn-primary" value="Запустить проверку">
</form>
<table class="table table-bordered table-responsive" data-test="checks">
Expand All @@ -35,30 +35,28 @@
</tr>
</thead>
<tbody>
<?php if (isset($checks)) : ?>
<?php foreach ($checks as $check) : ?>
<tr>
<td>
<?= htmlspecialchars($check->id) ?>
</td>
<td>
<?= htmlspecialchars($check->status_code ?? '') ?>
</td>
<td>
<?= htmlspecialchars($check->h1 ?? '') ?>
</td>
<td>
<?= htmlspecialchars($check->title ?? '') ?>
</td>
<td>
<?= htmlspecialchars($check->description ?? '') ?>
</td>
<td>
<?= htmlspecialchars($check->created_at) ?>
</td>
</tr>
<?php endforeach ?>
<?php endif ?>
<?php foreach ($checks as $check) : ?>
<tr>
<td>
<?= htmlspecialchars($check->id) ?>
</td>
<td>
<?= htmlspecialchars($check->status_code ?? '') ?>
</td>
<td>
<?= htmlspecialchars($check->h1 ?? '') ?>
</td>
<td>
<?= htmlspecialchars($check->title ?? '') ?>
</td>
<td>
<?= htmlspecialchars($check->description ?? '') ?>
</td>
<td>
<?= htmlspecialchars($check->created_at) ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>

0 comments on commit 094e73c

Please sign in to comment.