在Slim Framework中,管理跨多个路由共享的数据的最佳方式是什么



假设您有3个页面、3条路由(/index/about/contact(和一个共享标头,其中显示从数据库检索到的项目列表。

Slim中是否有更好的方法来检索所有页面/路由的这些项目,并将其传递到相应的模板,而不是为每个路由控制器方法复制代码?

例如,除此之外还有其他方式吗?

$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) */
]);
});

共享相同数据的路由也可以使用相同的响应程序来呈现内容。

伪示例:

<?php
namespace AppResponder;
use PsrHttpMessageResponseInterface;
use SlimViewsTwig;
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);
}
}

使用

$this->responder->render($response, 'index.twig', ['page' => 'content']);

或者。。。

use AppResponderResponder;
// ...
$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']);
});

一个选项是将这些项作为全局变量添加到TwigEnvironment中,以便它们在每个模板中都可用。

首先,您需要向Twig环境添加全局变量:

// 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;
});

现在变量items可用于每个Twig模板,而无需将其显式传递给render方法:

布局.titch

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 %}

所有三个模板index.twitch、about.twig和contact.titch都可以扩展布局.titch:

{% extends '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']);
});

最新更新