簡體   English   中英

Slim Framework在twig中訪問flash消息

[英]Slim Framework accessing the flash message in twig

我正在使用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]}}的原因,但它仍然沒有用。

我在這里錯過了什么嗎?

通過添加中間件來實現它

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

    return $response;
});

然后在我的模板中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM