How to use flash messaging with redirects in slim 3 and twig
<p>I'm learning to use the slim framework (v3) and I'm stuck with trying to make a redirect with a flash message. I have a home URL and a /flash URI that should set a flash message and redirect to home.</p>

<p>My bootstrap code is:</p>

<pre><code>use Slim\App;
use Slim\Container;
use Slim\Flash;
$container = new Container(['settings' =&gt; ['displayErrorDetails' =&gt;  true]]);
$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig([
        INC_ROOT. '/app/views',
        INC_ROOT. '/app/views/templates',
        INC_ROOT. '/app/views/templates/partials',], [
            'debug' =&gt; true
        ]);
    $view-&gt;addExtension(new \Slim\Views\TwigExtension(
        $container['router'],
        $container['request']-&gt;getUri()
    ));

    $view-&gt;getEnvironment()-&gt;addGlobal('flash', $container['flash']);

    return $view;
};
$container['flash'] = function () {
    return new Flash\Messages();
};
$app = new App($container);
</code></pre>

<p>My routing file is:</p>

<pre><code>$app-&gt;get('/', function($request, $response, $args){
    return $this-&gt;view-&gt;render($response, 'home.twig');
})-&gt;setName('home');

$app-&gt;get('/flash', function ($req, $res, $args) {
    // Set flash message for next request
    $this-&gt;flash-&gt;addMessage('global', 'This is a message');

    // Redirect
    return $res-&gt;withStatus(301)-&gt;withHeader("Location", $this-&gt;router-&gt;pathFor('home'));
});
</code></pre>

<p>And I'm trying to use the message in twig as:</p>

<pre><code>{{ flash.getMessage('global')[0] }}
</code></pre>

<p>The redirect is happening, but the flash message is not been passed to the new location. What I'm I doing wrong?</p>