Skip to content

Commit

Permalink
add routeName
Browse files Browse the repository at this point in the history
  • Loading branch information
Smol-An committed Jan 29, 2024
1 parent 5c74d43 commit 0957baf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
require __DIR__ . '/../vendor/autoload.php';

use Slim\Factory\AppFactory;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Routing\RouteContext;
use DI\Container;
use Valitron\Validator;
use Carbon\Carbon;
Expand All @@ -19,6 +22,18 @@
$container = new Container();

$app = AppFactory::createFromContainer($container);

$app->add(function (Request $request, RequestHandler $handler) use ($container) {
$routeContext = RouteContext::fromRequest($request);
$route = $routeContext->getRoute();

$routeName = !empty($route) ? $route->getName() : '';
$container->set('routeName', $routeName);

return $handler->handle($request);
});

$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);

$container->set('router', $app->getRouteCollector()->getRouteParser());
Expand All @@ -32,6 +47,7 @@
});
$container->set('renderer', function () use ($container) {
$templateVariables = [
'routeName' => $container->get('routeName'),
'router' => $container->get('router'),
'flash' => $container->get('flash')->getMessages()
];
Expand Down
4 changes: 2 additions & 2 deletions templates/header.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link"
<a class="nav-link <?= $routeName === 'home' ? 'active' : '' ?>"
href="<?= $router->urlFor('home') ?>">
Главная
</a>
</li>
<li class="nav-item">
<a class="nav-link"
<a class="nav-link <?= $routeName === 'urls' ? 'active' : '' ?>"
href="<?= $router->urlFor('urls') ?>">
Сайты
</a>
Expand Down

0 comments on commit 0957baf

Please sign in to comment.