【问题标题】:Slim Framework accessing the flash message in twigSlim 框架访问 twig 中的 flash 消息
【发布时间】:2016-11-20 23:53:05
【问题描述】:

我使用的是 Slim 3,无法访问我的 twig 模板中的 flash 消息 (slim/flash)。这是我的代码:

路由器:

    $this->flash->addMessage('msg', 'Account with ID '.$id.' has been deleted.');
    return $res->withStatus(302)->withHeader('Location', '/dashboard/accounts');

模板:

{{ ["slimFlash"]["msg"][0] }}

虽然我尝试print_r($_SESSION),但我没有收到任何错误,这就是我得到的:

[slimFlash] => Array
    (
        [msg] => Array
            (
                [0] => Account with ID 1 has been deleted.
            )

    )

这就是为什么我尝试了 {{ ["slimFlash"]["msg"][0] }} 但它仍然不起作用。

我错过了什么吗?

【问题讨论】:

    标签: php arrays twig slim


    【解决方案1】:

    通过添加中间件使其工作

    $app->add(function ($request, $response, $next) {
        $this->view->offsetSet("flash", $this->flash->getMessages());
        $response = $next($request, $response);
    
        return $response;
    });
    

    然后在我的模板中

    {{ flash["msg"][0] }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多