第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 Slim Framework 中管理跨多個(gè)路由共享的數(shù)據(jù)的最佳方法是什么?

在 Slim Framework 中管理跨多個(gè)路由共享的數(shù)據(jù)的最佳方法是什么?

PHP
不負(fù)相思意 2023-09-15 10:14:12
假設(shè)您有 3 個(gè)頁(yè)面、3 個(gè)路由 ( /index、/about、/contact) 和一個(gè)顯示從數(shù)據(jù)庫(kù)檢索的項(xiàng)目列表的共享標(biāo)頭。Slim 中是否有更好的方法來(lái)檢索所有頁(yè)面/路由的這些項(xiàng)目,并將其傳遞給相應(yīng)的模板,而不是本質(zhì)上為每個(gè)路由控制器方法復(fù)制代碼?例如,除此之外還有其他方法嗎?$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) */    ]);});
查看完整描述

2 回答

?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

共享相同數(shù)據(jù)的路由也可以使用相同的響應(yīng)程序來(lái)呈現(xiàn)內(nèi)容。


偽示例:


<?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);

    }

}

用法


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

或者...


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']);

});


查看完整回答
反對(duì) 回復(fù) 2023-09-15
?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊

一種選擇是將這些項(xiàng)目作為全局變量添加到 TwigEnvironment,以便它們?cè)诿總€(gè)模板中可用。


首先,您需要將全局變量添加到Twig環(huán)境中:


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

});

現(xiàn)在變量items可用于每個(gè) Twig 模板,而無(wú)需將其顯式傳遞給render方法:


布局.樹(shù)枝:


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

所有三個(gè)模板index.twig,about.twig和contact.twig都可以擴(kuò)展layout.twig:


{% extends 'layout.twig' %}

對(duì)于layout.twig中使用的相同變量,每個(gè)路由具有不同值的路由定義:


$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']);

});


查看完整回答
反對(duì) 回復(fù) 2023-09-15
  • 2 回答
  • 0 關(guān)注
  • 142 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)