diff --git a/config/autoload/navigation.global.php b/config/autoload/navigation.global.php new file mode 100644 index 00000000..3c4517f2 --- /dev/null +++ b/config/autoload/navigation.global.php @@ -0,0 +1,169 @@ + [ + //enable menu item active if any child is active + 'active_recursion' => true, + 'containers' => [ + 'left_menu' => [ + 'type' => 'ArrayProvider', + 'options' => [ + 'items' => [ + [ + 'options' => [ + 'label' => 'Pages', + 'route' => [], + ], + 'attributes' => [ + 'class' => 'nav-link dropdown-toggle', + 'href' => '#', + ], + 'pages' => [ + [ + 'options' => [ + 'label' => 'Home', + 'uri' => '/home', + ], + 'attributes' => [ + 'class' => 'dropdown-item', + ], + ], + [ + 'options' => [ + 'label' => 'About Us', + 'uri' => '/page/about-us', + ], + 'attributes' => [ + 'class' => 'dropdown-item', + ], + ], + [ + 'options' => [ + 'label' => 'Who We Are', + 'uri' => '/page/who-we-are', + ], + 'attributes' => [ + 'class' => 'dropdown-item', + ], + ], + [ + 'options' => [ + 'label' => 'Premium content', + 'uri' => '/page/premium-content', + ], + 'attributes' => [ + 'class' => 'dropdown-item', + ], + ], + ], + ], + [ + 'options' => [ + 'label' => 'Contribute', + 'uri' => 'https://github.com/dotkernel', + ], + 'attributes' => [ + 'class' => 'nav-link', + 'target' => '_blank', + ], + ], + [ + 'options' => [ + 'label' => 'Contact Us', + 'uri' => '/contact/form', + ], + 'attributes' => [ + 'class' => 'nav-link', + ], + ], + [ + 'options' => [ + 'label' => 'Disabled', + 'uri' => '/', + ], + 'attributes' => [ + 'class' => 'nav-link disabled', + ], + ], + ], + ], + ], + 'guest_menu' => [ + 'type' => 'ArrayProvider', + 'options' => [ + 'items' => [ + [ + 'options' => [ + 'label' => 'Log in', + 'uri' => '/user/login', + ], + 'attributes' => [ + 'class' => 'nav-link', + ], + ], + ], + ], + ], + 'user_menu' => [ + 'type' => 'ArrayProvider', + 'options' => [ + 'items' => [ + [ + 'options' => [ + 'label' => 'Profile', + 'uri' => '/account/details', + ], + 'attributes' => [ + 'class' => 'nav-link', + ], + ], + [ + 'options' => [ + 'label' => 'Log out', + 'uri' => '/user/logout', + ], + 'attributes' => [ + 'class' => 'nav-link', + ], + ], + ], + ], + ], + 'user_profile_menu' => [ + 'type' => 'ArrayProvider', + 'options' => [ + 'items' => [ + [ + 'options' => [ + 'label' => 'Avatar', + 'uri' => '/account/avatar', + ], + ], + [ + 'options' => [ + 'label' => 'Details', + 'uri' => '/account/details', + ], + ], + [ + 'options' => [ + 'label' => 'Change password', + 'uri' => '/account/change-password', + ], + ], + [ + 'options' => [ + 'label' => 'Delete account', + 'uri' => '/account/delete-account', + ], + ], + ], + ], + ], + ], + //register custom providers here + 'provider_manager' => [], + ], +]; diff --git a/config/autoload/templates.global.php b/config/autoload/templates.global.php index 56308edf..7ee92fac 100644 --- a/config/autoload/templates.global.php +++ b/config/autoload/templates.global.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Dot\Twig\Extension\DateExtension; +use Frontend\App\Twig\Extension\RouteExtension; use Laminas\ServiceManager\Factory\InvokableFactory; use Mezzio\Template\TemplateRendererInterface; use Mezzio\Twig\TwigEnvironmentFactory; @@ -29,12 +30,13 @@ 'cache_dir' => 'data/cache/twig', 'extensions' => [ DateExtension::class, + RouteExtension::class, ], 'optimizations' => -1, 'runtime_loaders' => [], //'timezone' => '', 'globals' => [ - 'appName' => $app['name'], + 'appName' => $app['name'] ?? '', ], ], ]; diff --git a/config/config.php b/config/config.php index 6686a4ce..fc5dda8a 100644 --- a/config/config.php +++ b/config/config.php @@ -45,6 +45,8 @@ class_exists(\Mezzio\Swoole\ConfigProvider::class) \Dot\ResponseHeader\ConfigProvider::class, \Dot\DataFixtures\ConfigProvider::class, \Dot\Cache\ConfigProvider::class, + \Dot\Helpers\ConfigProvider::class, + \Dot\Navigation\ConfigProvider::class, // Default App module config \Frontend\App\ConfigProvider::class, diff --git a/config/pipeline.php b/config/pipeline.php index 3915570f..c3859ece 100644 --- a/config/pipeline.php +++ b/config/pipeline.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Dot\ErrorHandler\ErrorHandlerInterface; +use Dot\Navigation\NavigationMiddleware; use Dot\Rbac\Guard\Middleware\ForbiddenHandler; use Dot\Rbac\Guard\Middleware\RbacGuardMiddleware; use Dot\ResponseHeader\Middleware\ResponseHeaderMiddleware; @@ -77,6 +78,7 @@ $app->pipe(AuthMiddleware::class); $app->pipe(ForbiddenHandler::class); $app->pipe(RbacGuardMiddleware::class); + $app->pipe(NavigationMiddleware::class); // Register the dispatch middleware in the middleware pipeline $app->pipe(DispatchMiddleware::class); diff --git a/src/App/src/ConfigProvider.php b/src/App/src/ConfigProvider.php index de61c488..193f20f1 100644 --- a/src/App/src/ConfigProvider.php +++ b/src/App/src/ConfigProvider.php @@ -36,6 +36,7 @@ public function getDependencies(): array RecaptchaService::class => AttributedServiceFactory::class, CookieService::class => AttributedServiceFactory::class, RememberMeMiddleware::class => AttributedServiceFactory::class, + Twig\Extension\RouteExtension::class => AttributedServiceFactory::class, ], 'aliases' => [ EntityManager::class => 'doctrine.entity_manager.orm_default', diff --git a/src/App/src/Twig/Extension/RouteExtension.php b/src/App/src/Twig/Extension/RouteExtension.php new file mode 100644 index 00000000..a7b35848 --- /dev/null +++ b/src/App/src/Twig/Extension/RouteExtension.php @@ -0,0 +1,46 @@ +urlHelper->getRequest()?->getUri()?->getPath(); + } + + public function isRoute(?string $route): bool + { + if (null === $route) { + return false; + } + + $currentRoute = $this->getCurrentRoute(); + if (null === $currentRoute) { + return false; + } + + return $currentRoute === $route; + } +} diff --git a/src/App/templates/layout/default.html.twig b/src/App/templates/layout/default.html.twig index 6ae7dbf4..d0e0e7d0 100644 --- a/src/App/templates/layout/default.html.twig +++ b/src/App/templates/layout/default.html.twig @@ -42,37 +42,14 @@
diff --git a/src/App/templates/partial/menu.html.twig b/src/App/templates/partial/menu.html.twig new file mode 100644 index 00000000..6235e29d --- /dev/null +++ b/src/App/templates/partial/menu.html.twig @@ -0,0 +1,27 @@ +{% set extraAttributes = '' %} +{% if page is defined %} + +{% endif %} diff --git a/src/App/templates/partial/user_profile_menu.html.twig b/src/App/templates/partial/user_profile_menu.html.twig new file mode 100644 index 00000000..42057267 --- /dev/null +++ b/src/App/templates/partial/user_profile_menu.html.twig @@ -0,0 +1,8 @@ +{% set extraAttributes = '' %} +{% for page in container %} + {% if navigation.isAllowed(page) %} +