Php 在Slim框架中,管理跨多个路由共享的数据的最佳方法是什么?
假设您有3个页面、3条路线(Php 在Slim框架中,管理跨多个路由共享的数据的最佳方法是什么?,php,slim,Php,Slim,假设您有3个页面、3条路线(/index、/about、/contact)和一个共享标题,其中显示从数据库检索的项目列表 在Slim中,有没有更好的方法来检索所有页面/路由的这些项,并传递到相应的模板,而不是为每个路由控制器方法复制代码 e、 除此之外还有别的办法吗 $app->get('/', function ($request, $response, $args) { return $this->view->render($response, 'index.twi
/index
、/about
、/contact
)和一个共享标题,其中显示从数据库检索的项目列表
在Slim中,有没有更好的方法来检索所有页面/路由的这些项,并传递到相应的模板,而不是为每个路由控制器方法复制代码
e、 除此之外还有别的办法吗
$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) */
]);
});
一种选择是将这些项作为全局变量添加到TwiGenEnvironment,以便它们在每个模板中都可用 首先,您需要向Twig环境添加全局变量:
//一个简单的项目提供程序,可帮助您生成项目列表
//您可以将其更改为从数据库中读取项目等。
类项目提供者{
公共函数getItems()
{
返回[‘第1项’、‘第2项’、‘第3项’、‘第4项’、];
}
}
//在容器中设置视图
$container->set('view',函数($c){
$twig=twig::create(“一个选项是将这些项作为全局变量添加到TwigEnvironment,以便它们在每个模板中都可用
首先,您需要向Twig环境添加全局变量:
//一个简单的项目提供程序,可帮助您生成项目列表
//您可以将其更改为从数据库中读取项目等。
类项目提供者{
公共函数getItems()
{
返回[‘第1项’、‘第2项’、‘第3项’、‘第4项’、];
}
}
//在容器中设置视图
$container->set('view',函数($c){
$twig=twig::create(“共享相同数据的路由也可以使用相同的响应程序来呈现内容
伪示例:
<?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);
}
}
或者
使用App\Responder\Responder;
// ...
$app->get('/index',函数($request,$response,$args){
return$this->get(Responder::class)->render($response,'index.twig',['pagecontent'=>'这是一些仅用于/index']的内容);
});
$app->get('/about',函数($request、$response、$args){
返回$this->get(Responder::class)->render($response,'about.twig',['pagecontent'=>'您可以为/about']自定义内容);
});
$app->get('/contact',函数($request,$response,$args){
返回$this->get(Responder::class)->render($response,'contact.twig',['pagecontent'=>'…和/contact以及']);
});
共享相同数据的路由也可以使用相同的响应程序来呈现内容
伪示例:
<?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);
}
}
或者
使用App\Responder\Responder;
// ...
$app->get('/index',函数($request,$response,$args){
return$this->get(Responder::class)->render($response,'index.twig',['pagecontent'=>'这是一些仅用于/index']的内容);
});
$app->get('/about',函数($request、$response、$args){
返回$this->get(Responder::class)->render($response,'about.twig',['pagecontent'=>'您可以为/about']自定义内容);
});
$app->get('/contact',函数($request,$response,$args){
返回$this->get(Responder::class)->render($response,'contact.twig',['pagecontent'=>'…和/contact以及']);
});
您使用的是哪个版本的Slim?从视图名称猜,您使用的是Twig,对吗?我的印象是,这可能是Slim版本不可知的,但Slim 4和Twig 3是的。不同的解决方案可能依赖于框架,比如注册服务以在需要时提供此公共数据,或者像他们可能的那样不依赖于框架ld使用同一个响应者。@odan感谢您的建议。是否需要详细说明?您使用的是哪个版本的Slim?从视图名称猜测,您使用的是Twig,对吗?我的印象是,这可能是Slim版本不可知,但Slim 4和Twig 3,是的。不同的解决方案可能依赖于框架,例如注册要提供的服务无论何时需要这些通用数据,或者框架不可知,就像他们可以使用同一个响应者一样。@odan谢谢你的建议。想详细说明一下吗?
<?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);
}
}