mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2025-09-09 11:27:13 +02:00
Implemented Navigation
This commit is contained in:
67
src/controller/AbstractDefaultController.php
Normal file
67
src/controller/AbstractDefaultController.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
namespace controller;
|
||||
|
||||
use router\link\Link;
|
||||
use router\Router;
|
||||
|
||||
/**
|
||||
* This controllers render the frames/default.html.twig
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*/
|
||||
class AbstractDefaultController extends AbstractController
|
||||
{
|
||||
|
||||
protected function render(string $template, array $variables = []): void
|
||||
{
|
||||
parent::render($template, $this->addMenuItems($variables));
|
||||
}
|
||||
|
||||
private function addMenuItems(array $variables): array
|
||||
{
|
||||
if (array_key_exists('menu_items', $variables)) {
|
||||
throw new \Exception('You aren\'t allowed to define this key!');
|
||||
}
|
||||
$variables['menu_items'] = $this->getMenuItems();
|
||||
return $variables;
|
||||
}
|
||||
|
||||
private function getMenuItems(): array
|
||||
{
|
||||
return array_merge([
|
||||
new Link([], 'home'),
|
||||
new Link([
|
||||
Router::CONTROLLER => 'product',
|
||||
Router::ACTION => 'list'
|
||||
], 'product list'),
|
||||
new Link([
|
||||
Router::CONTROLLER => 'order',
|
||||
Router::ACTION => 'basket'
|
||||
], 'basket')
|
||||
], $this->getUserMenuItems());
|
||||
}
|
||||
|
||||
private function getUserMenuItems(): array
|
||||
{
|
||||
if ($this->core->getUser()) {
|
||||
return [
|
||||
new Link([
|
||||
Router::CONTROLLER => 'user',
|
||||
Router::ACTION => 'logout'
|
||||
], 'logout')
|
||||
];
|
||||
}
|
||||
return [
|
||||
new Link([
|
||||
Router::CONTROLLER => 'user',
|
||||
Router::ACTION => 'login'
|
||||
], 'login'),
|
||||
new Link([
|
||||
Router::CONTROLLER => 'user',
|
||||
Router::ACTION => 'register'
|
||||
], 'register')
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user