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

时间:2013-01-11 16:51:11

标签: 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 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"
    }
}

1 个答案:

答案 0 :(得分:1)

如果没有更多细节,很难回答这个问题。

首先,请注意,过去出现过类似的例外情况,原因是Symfony2.1错误已在最近发布的版本中得到解决:see here for more details

然后,请注意错误似乎在类addGlobal的{​​{1}}函数中抛出。你安装了这样的Twig Extension吗?

如果没有阅读Twig_Environment,我建议您尝试:

  1. 清除缓存。
  2. 更新 composer.json中的代码,以避免出现最新的捆绑包。
  3. 创建composer.json
  4. 然后编译它以获得composer.phar
  5. composer.pharphp composer.phar install,无论你的情况如何。
  6. 希望它有所帮助。