在 Symfony 中,我习惯于在任何 View 中使用 {{ app.user.username }}
。我可以在 Slim 中做这样的事情吗?
目前我将用户添加到 $container['user']
并使用 view->render($response, 'index.html.twig' 将其传递到每个 View 中, ['user' => $this->user])
.
换句话说,我可以将 user
添加为 Twig 的全局变量吗?我看到了许多类似的问题,但没有我能够使用的解决方案。
最佳答案
$container['view']->addGlobal('user', $this->user);
怎么样?
关于php - 如何在 Slim 3.5 中为 Twig 添加全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202717/