I have a website build with Slim PHP (4). The site has a mega menu which we want to build dynamically from a database.
The site has a base template (base.twig) which in turn loads the header (header.twig) as well as other sections and has a block where the content for each page will load.
{% include 'partials/header.twig' %}
{% include 'partials/flash.twig' %}
{% block content %}{% endblock %}
{% include 'partials/social.twig' %}
{% include 'partials/newsletter.twig' %}
{% include 'partials/footer.twig' %}
I created a controller which runs a query on the database to check which shows we currently have active, then it sorts them into categories and then returns an array with the home template. When the home page loads, the mega menu is dynamically built with this information. This is working as expected.
class HomeController extends Controller {
public function index($request, $response) {
$queryShows = "SELECT showName AS name, CONCAT('shows/', showPath) AS path, cat AS category FROM shows WHERE active = 1 ORDER BY showName";
$showsQuery = $this->c->get('db')->prepare($queryShows);
$showsQuery->execute();
$allShows = $showsQuery->fetchAll(PDO::FETCH_ASSOC);
$queryMovies = "SELECT movieName AS name, CONCAT('movies/', filename) AS path, cat AS category FROM movies WHERE active = 1 ORDER BY movieName";
$moviesQuery = $this->c->get('db')->prepare($queryMovies);
$moviesQuery->execute();
$allMovies = $moviesQuery->fetchAll(PDO::FETCH_ASSOC);
$allCategories = $this->sortLibrary($allShows, $allMovies);
return $this->c->get('view')->render($response, 'home.twig', [
'categories' => $allCategories
]);
}
}
The problem is that I did this on the HomeController. And because of this, the mega menu only works on the home page. As soon as I move to a different page, the mega menu is empty.
How can I get the mega menu data to load into the partials/header.twig template without having to include this code in every single controller for every single page on the site?