Compare commits
2 Commits
e0d6563583
...
408cc0d88a
| Author | SHA1 | Date | |
|---|---|---|---|
| 408cc0d88a | |||
| edda567057 |
32
src/EventSubscriber/TwigEventSubscriber.php
Normal file
32
src/EventSubscriber/TwigEventSubscriber.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\EventSubscriber;
|
||||||
|
|
||||||
|
use App\Repository\ConferenceRepository;
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
||||||
|
use Twig\Environment;
|
||||||
|
|
||||||
|
class TwigEventSubscriber implements EventSubscriberInterface
|
||||||
|
{
|
||||||
|
private $twig;
|
||||||
|
private $conferenceRepository;
|
||||||
|
|
||||||
|
public function __construct(Environment $twig, ConferenceRepository $conferenceRepository)
|
||||||
|
{
|
||||||
|
$this->twig = $twig;
|
||||||
|
$this->conferenceRepository = $conferenceRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onControllerEvent(ControllerEvent $event): void
|
||||||
|
{
|
||||||
|
$this->twig->addGlobal('conferences', $this->conferenceRepository->findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getSubscribedEvents(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
ControllerEvent::class => 'onControllerEvent',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,12 @@ class ConferenceRepository extends ServiceEntityRepository
|
|||||||
parent::__construct($registry, Conference::class);
|
parent::__construct($registry, Conference::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function findAll(): array
|
||||||
|
{
|
||||||
|
// Override findAll() to use custom sorting
|
||||||
|
return $this->findBy([], ['year' => 'ASC', 'city' => 'ASC']);
|
||||||
|
}
|
||||||
|
|
||||||
// /**
|
// /**
|
||||||
// * @return Conference[] Returns an array of Conference objects
|
// * @return Conference[] Returns an array of Conference objects
|
||||||
// */
|
// */
|
||||||
|
|||||||
@@ -12,6 +12,15 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1><a href="{{ path('homepage') }}">Guestbook</a></h1>
|
||||||
|
<ul>
|
||||||
|
{%for conference in conferences %}
|
||||||
|
<li><a href="{{ path('conference', { id: conference.id }) }}">{{ conference }}</a></li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
<hr/>
|
||||||
|
</header>
|
||||||
{% block body %}{% endblock %}
|
{% block body %}{% endblock %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user