【问题标题】:Symfony 2.8 | Twig | app.user and app.session null in stage env, ok in local and prodSymfony 2.8 |树枝 | app.user 和 app.session 在 stage env 中为 null,在本地和 prod 中正常
【发布时间】:2020-09-25 14:40:24
【问题描述】:

我正在开发一个使用 Symfony 2.8 的旧项目。

我在本地开发,在 Twig 中我可以正确使用{{app.user.username}} 或其他类似{{app.session...}}。 当我将它部署在 暂存环境 中时,这些值始终为空白。 然后,如果我将它部署在生产中,全局app 变量就像在本地一样工作。

暂存环境是否可能配置为不初始化app 变量?我在哪里可以看看?

另外,一件奇怪的事情。如果我进行自定义,从控制器加载会话数据并将它们传递给视图,也会发生相同的行为。

控制器

$session = $this->get('session');

$content = $this->render(
            'blocks/logged_user_menu.html.twig',
            [
                'access_token_sc' => $session->get('access_token')
            ]
        );

查看

{{ access_token_sc }}

我读到我可以看看Security config,但似乎没有帮助。

【问题讨论】:

  • 听起来会话存储/处理程序有所不同。 app/config/config.yml 中的会话属性是什么?
  • 感谢您的提示,我找到了config.ymlsession handler_id: session.handler.pdo 和一个config_test.yml,其中包括第一个,然后是session: storage_id: session.storage.mock_file...我会在这个方向上尝试一下
  • oyu 尝试过什么调试问题?为什么不将您的本地环境设置为staging 并启动像 XDebug 这样的调试器?

标签: php symfony twig session-variables symfony-2.8


【解决方案1】:

感谢@john Smith 的评论,我发现这种问题与config.yml 文件中的session.storage_id 有关。在我的例子中,测试配置文件使用了mock_file,它禁止了会话行为。

此处解释说明供参考: MockFileSessionStorage class

namespace Symfony\Component\HttpFoundation\Session\Storage;

/**
 * MockFileSessionStorage is used to mock sessions for
 * functional testing when done in a single PHP process.
 *
 * No PHP session is actually started since a session can be initialized
 * and shutdown only once per PHP execution cycle and this class does
 * not pollute any session related globals, including session_*() functions
 * or session.* PHP ini directives.
 *
 * @author Drak <drak@zikula.org>
 */
class MockFileSessionStorage extends MockArraySessionStorage
{ ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    相关资源
    最近更新 更多