将同一个数组传递给多个路由的更好方法
Better way to pass the same array to multiple routes
我正在寻找将同一个数组传递给多个路由模板的推荐方法。
示例:
$app->group('/blabla', function (RouteCollectorProxy $group) {
$group->get('', function (Request $request, Response $response, $args) {
return $this->get('view')->render($response, 'first-route.html.twig', [
'var1' => "wiggle",
'var2' => "lightSpeedIn",
'var3' => 'fadeInDown',
'var4' => 'wiggle',
'var5' => "wobble",
]);
})->setName('portfolio-ancien');
$group->get('/hello', function (Request $request, Response $response, $args) {
return $this->get('view')->render($response, 'seconde-route.html.twig', [
'var1' => "wiggle",
'var2' => "lightSpeedIn",
'var3' => 'fadeInDown',
'var4' => 'wiggle',
'var5' => "wobble",
]);
})->setName('portfolio-ancien-quiSuisJe');
});
我的数组参数是一样的,如何传给模板而不重复两次?
您可以在组级别定义它们,然后将它们传递给具有 use
...
的路由
$app->group('/blabla', function (RouteCollectorProxy $group) {
$vars = [
'var1' => "wiggle",
'var2' => "lightSpeedIn",
'var3' => 'fadeInDown',
'var4' => 'wiggle',
'var5' => "wobble",
];
$group->get('', function (Request $request, Response $response, $args) use ($vars) {
return $this->get('view')->render($response, 'first-route.html.twig', $vars);
})->setName('portfolio-ancien');
$group->get('/hello', function (Request $request, Response $response, $args) use ($vars) {
return $this->get('view')->render($response, 'seconde-route.html.twig', $vars);
})->setName('portfolio-ancien-quiSuisJe');
});
我正在寻找将同一个数组传递给多个路由模板的推荐方法。
示例:
$app->group('/blabla', function (RouteCollectorProxy $group) {
$group->get('', function (Request $request, Response $response, $args) {
return $this->get('view')->render($response, 'first-route.html.twig', [
'var1' => "wiggle",
'var2' => "lightSpeedIn",
'var3' => 'fadeInDown',
'var4' => 'wiggle',
'var5' => "wobble",
]);
})->setName('portfolio-ancien');
$group->get('/hello', function (Request $request, Response $response, $args) {
return $this->get('view')->render($response, 'seconde-route.html.twig', [
'var1' => "wiggle",
'var2' => "lightSpeedIn",
'var3' => 'fadeInDown',
'var4' => 'wiggle',
'var5' => "wobble",
]);
})->setName('portfolio-ancien-quiSuisJe');
});
我的数组参数是一样的,如何传给模板而不重复两次?
您可以在组级别定义它们,然后将它们传递给具有 use
...
$app->group('/blabla', function (RouteCollectorProxy $group) {
$vars = [
'var1' => "wiggle",
'var2' => "lightSpeedIn",
'var3' => 'fadeInDown',
'var4' => 'wiggle',
'var5' => "wobble",
];
$group->get('', function (Request $request, Response $response, $args) use ($vars) {
return $this->get('view')->render($response, 'first-route.html.twig', $vars);
})->setName('portfolio-ancien');
$group->get('/hello', function (Request $request, Response $response, $args) use ($vars) {
return $this->get('view')->render($response, 'seconde-route.html.twig', $vars);
})->setName('portfolio-ancien-quiSuisJe');
});