在 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) */

    ]);

});


不负相思意
浏览 93回答 2
2回答

人到中年有点甜

共享相同数据的路由也可以使用相同的响应程序来呈现内容。伪示例:<?phpnamespace App\Responder;use Psr\Http\Message\ResponseInterface;use Slim\Views\Twig;final class Responder{&nbsp; &nbsp; private $twig;&nbsp; &nbsp; public function __construct(Twig $twig)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->twig = $twig;&nbsp; &nbsp; }&nbsp; &nbsp; public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $shared = ['item1', 'item2'];&nbsp; &nbsp; &nbsp; &nbsp; $data = array_replace(shared, $data);&nbsp; &nbsp; &nbsp; &nbsp; return $this->twig->render($response, $template, $data);&nbsp; &nbsp; }}用法$this->responder->render($response, 'index.twig', ['page' => 'content']);或者...use App\Responder\Responder;// ...$app->get('/index', function($request, $response, $args){&nbsp; &nbsp; return $this->get(Responder::class)->render($response, 'index.twig', ['pagecontent' => 'This is some content for /index only']);});$app->get('/about', function($request, $response, $args){&nbsp; &nbsp; return $this->get(Responder::class)->render($response, 'about.twig', ['pagecontent' => 'You can have custom content for /about']);});$app->get('/contact', function($request, $response, $args){&nbsp; &nbsp; 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 {&nbsp; &nbsp; public function getItems()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return ['item 1', 'item 2', 'item 3', 'item-4', ];&nbsp; &nbsp; }}// Set view in Container$container->set('view', function($c) {&nbsp; &nbsp; $twig = Twig::create('<path-to-tiwg-templates'>);&nbsp; &nbsp; // get items from items-provider and add them as global `items` variable&nbsp; &nbsp; // to all twig templates&nbsp; &nbsp; $twig->getEnvironment()->addGlobal('items', $c->get('items-provider')->getItems());&nbsp; &nbsp; return $twig;});// set sample items, you can modifiy this&nbsp;$container->set('items-provider', function($c){&nbsp; &nbsp; return new ItemsProvider;});现在变量items可用于每个 Twig 模板,而无需将其显式传递给render方法:布局.树枝:These are some items available in every twig template:<br><ul>{% for item in items%}&nbsp; &nbsp; <li>{{ item }}</li>{% endfor %}</ul><br>And this is some page specific content:<br>{% block content %}&nbsp; &nbsp; {{ pagecontent }}{% endblock %}所有三个模板index.twig,about.twig和contact.twig都可以扩展layout.twig:{% extends 'layout.twig' %}对于layout.twig中使用的相同变量,每个路由具有不同值的路由定义:$app->get('/index', function($request, $response, $args){&nbsp; &nbsp; return $this->get('view')->render($response, 'index.twig', ['pagecontent' => 'This is some content for /index only']);});$app->get('/about', function($request, $response, $args){&nbsp; &nbsp; return $this->get('view')->render($response, 'about.twig', ['pagecontent' => 'You can have custom content for /about']);});$app->get('/contact', function($request, $response, $args){&nbsp; &nbsp; return $this->get('view')->render($response, 'contact.twig', ['pagecontent' => '...and /contact as well']);});
打开App,查看更多内容
随时随地看视频慕课网APP