Making data available in all routes/views

Today I found myself implementing history breadcrumbs in the in house CRM, built with SLIM 3, and realised that if I have the breadcrumbs in my php header (which is an include) that all works fine for the home page as that’s the route I’m getting the data in, and then passing it to the rendered view.

But I don’t really want repeat the SQL (via Idiorm) in every route just to get the breadcrumbs to work. After looking at dependancies and middleware I’ve still not managed to work out how to have the same data available in all routes.

Is this possible and if so how? In my Object Pascal days I’d just override the base class… maybe that’s a clue to myself!

I’ll keep working on it this evening…


If I understood correctly you want to set a global var to your View, Am I correct?

If so, and if you’re using Twig-View or Smarty-View, you can set global var just like that:

$view = $container->view; //Get your View from container
$view['key'] = 'value';

When you call $view->render, that var will be passed to your template.

Thanks MathMarques,

Almost. What I want to do is have the result set of a database query available in all views (I’m using PHPview) without having to duplicate the query (I’m using idiorm) 20 times across the web app for each view.

Maybe what you are suggesting works and I haven’t quite understood your answer.

OK, you helped a lot. Here’s my solution using SLIM 3 and slim/php-view where $history is an Idiorm result set.

$view = $container->renderer;
$view->addAttribute(‘history’, $history);

Thanks for your help.