php - What is the best way to manage data shared across multiple routes in Slim Framework?
Suppose you have 3 pages, 3 routes (/index
, /about
, /contact
) and a shared header which displays a list of items retrieved from the database.
Is there a better way in Slim to retrieve these items for all pages/routes, to pass to the corresponding templates, as opposed to essentially replicating code for each route controller method?
e.g., any way other than this?
$app->get('/', function ($request, $response, $args) {
return $this->view->render($response, 'index.twig', [
'items' => /* retrieve items from database */
]);
});
$app->get('/about', function ($request, $response, $args) {
return $this->view->render($response, 'about.twig', [
'items' => /* retrieve items from database (duplicate code) */
]);
});
$app->get('/contact', function ($request, $response, $args) {
return $this->view->render($response, 'contact.twig', [
'items' => /* retrieve items from database (duplicate code) */
]);
});
Answer
Solution:
Routes that share the same data could also use the same responder to render the content.
Pseudo example:
<?php
namespace App\Responder;
use Psr\Http\Message\ResponseInterface;
use Slim\Views\Twig;
final class Responder
{
private $twig;
public function __construct(Twig $twig)
{
$this->twig = $twig;
}
public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface
{
$shared = ['item1', 'item2'];
$data = array_replace(shared, $data);
return $this->twig->render($response, $template, $data);
}
}
Usage
$this->responder->render($response, 'index.twig', ['page' => 'content']);
or...
use App\Responder\Responder;
// ...
$app->get('/index', function($request, $response, $args){
return $this->get(Responder::class)->render($response, 'index.twig', ['pagecontent' => 'This is some content for /index only']);
});
$app->get('/about', function($request, $response, $args){
return $this->get(Responder::class)->render($response, 'about.twig', ['pagecontent' => 'You can have custom content for /about']);
});
$app->get('/contact', function($request, $response, $args){
return $this->get(Responder::class)->render($response, 'contact.twig', ['pagecontent' => '...and /contact as well']);
});
Answer
Solution:
One option is to add these items as global variables to TwigEnvironment, so that they are available in every template.
First, you need to add global variables to Twig environment:
// A simple items provider which helps you generate a list of items
// You can change this to something that reads the items from database, etc.
class ItemsProvider {
public function getItems()
{
return ['item 1', 'item 2', 'item 3', 'item-4', ];
}
}
// Set view in Container
$container->set('view', function($c) {
$twig = Twig::create('<path-to-tiwg-templates'>);
// get items from items-provider and add them as global `items` variable
// to all twig templates
$twig->getEnvironment()->addGlobal('items', $c->get('items-provider')->getItems());
return $twig;
});
// set sample items, you can modifiy this
$container->set('items-provider', function($c){
return new ItemsProvider;
});
Now variable items
is available to every Twig template without the need to pass it explicitly to render
method:
layout.twig:
These are some items available in every twig template:<br>
<ul>
{% for item in items%}
<li>{{ item }}</li>
{% endfor %}
</ul>
<br>
And this is some page specific content:<br>
{% block content %}
{{ pagecontent }}
{% endblock %}
All three templates index.twig, about.twig and contact.twig can extend layout.twig:
{% extends 'layout.twig' %}
Route definitions with different value per route for same variable used in layout.twig:
$app->get('/index', function($request, $response, $args){
return $this->get('view')->render($response, 'index.twig', ['pagecontent' => 'This is some content for /index only']);
});
$app->get('/about', function($request, $response, $args){
return $this->get('view')->render($response, 'about.twig', ['pagecontent' => 'You can have custom content for /about']);
});
$app->get('/contact', function($request, $response, $args){
return $this->get('view')->render($response, 'contact.twig', ['pagecontent' => '...and /contact as well']);
});
Source