我在 silex 应用程序中使用 Twig。在预请求 Hook 中,我想检查用户是否登录以及他们是否将用户对象添加到 Twig(这样我就可以在菜单中呈现登录/注销状态)。
然而,查看源代码后,它似乎只能提供模板 View 变量作为渲染方法的参数。我在这里错过了什么吗?
这正是我想要实现的目标:
// Code run on every request
$app->before(function (Request $request) use ($app)
{
// Check if the user is logged in and if they are
// Add the user object to the view
$status = $app['userService']->isUserLoggedIn();
if($status)
{
$user = $app['userService']->getLoggedInUser();
//@todo - find a way to add this object to the view
// without rendering it straight away
}
});
最佳答案
$app["twig"]->addGlobal("user", $user);
关于php - 在 Twig View 预渲染中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10108057/