Authentication twig中的app.user.username返回无法添加全局

Authentication twig中的app.user.username返回无法添加全局,authentication,symfony,twig,Authentication,Symfony,Twig,根据文件: http://symfony.com/doc/current/book/security.html#retrieving-the-user-object 我正在尝试使用twig检查用户是否经过身份验证 当我放入我的小树枝模板时: {{ app.user.username }} 我得到了错误 LogicException: Unable to add global "app" as the runtime or the extensions have already been in

根据文件:

http://symfony.com/doc/current/book/security.html#retrieving-the-user-object
我正在尝试使用twig检查用户是否经过身份验证

当我放入我的小树枝模板时:

{{ app.user.username }}
我得到了错误

LogicException: Unable to add global "app" as the runtime or the extensions have already been initialized.
编辑:请根据下面的composer.json内容查找

{
    "name": "symfony/framework-standard-edition",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.1.*",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.0.*",
        "twig/extensions": "1.0.*@dev",
        "symfony/assetic-bundle": "2.1.*",
        "symfony/swiftmailer-bundle": "2.1.*",
        "symfony/monolog-bundle": "2.1.*",
        "sensio/distribution-bundle": "2.1.*",
        "sensio/framework-extra-bundle": "2.1.*",
        "sensio/generator-bundle": "2.1.*",
        "jms/security-extra-bundle": "1.2.*",
        "jms/di-extra-bundle": "1.1.*",
        "kriswallsmith/assetic": "1.1.*@dev",
        "doctrine/data-fixtures": "dev-master",
        "doctrine/doctrine-fixtures-bundle": "dev-master"
    },
    "scripts": {
        "post-install-cmd": [
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ],
        "post-update-cmd": [
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ]
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web"
    }
}
{
“名称”:“symfony/framework标准版”,
“说明”:“Symfony标准版”发行版,
“自动加载”:{
“psr-0”:{”“:“src/”}
},
“要求”:{
“php”:“>=5.3.3”,
“symfony/symfony”:“2.1.*”,

“条令/orm”:“>=2.2.3,如果没有更多细节,很难回答这个问题

首先,请注意,由于一个
Symfony2.1
错误,在最近的版本中解决了一个类似的异常:

然后,请注意,该错误似乎是在类
Twig_Environment
addGlobal
函数中抛出的。您是否安装了这样的Twig扩展

不必阅读您的
composer.json
,我可以建议您尝试:

  • 清除缓存
  • 更新
    composer.json中的代码,以避免出现最新的捆绑包
  • 创建
    composer.phar
  • 然后编译它以获得
    composer.phar
  • php composer.phar安装
    php composer.phar更新
    ,无论您的情况如何

  • 希望能有所帮助。

    我会更新你的供应商并清除你的缓存。有些东西是不可靠的。好吧,唯一的办法是如果你有一个小树枝扩展,并且你使用了
    addGlobal('app','some value'))
    您使用的是这样一个非Sensiolab捆绑包吗?您使用的确切版本是什么?您可以发布您的composer.json吗?composer.json是正确的!您是否通过最新版本的composer生成composer.phar?更新和清除缓存是否有帮助?您可以找到类似的问题。