In this episode, we're focusing on improving how we handle flash messages in our Twig templates. Previously, we were dealing with passing messages through our controllers and managing sessions directly in multiple places. That's getting a bit messy, so it's time to tidy things up and make flash messaging available globally across our app.
We'll start by revisiting our Twig runtime extension and creating a new helper method that gives us access to the session object. With that in place, we register a new Twig function called session
, so we can access the session easily in any of our templates — no controller plumbing needed!
Then, instead of displaying flash messages only in certain places, we jump to our main app.twig
layout and add logic at the top so that flash messages display globally. This way, no matter where a message is set, it'll pop up at the top of the page after any action, like login or logout, and disappear after a refresh as expected. Plus, you can style it up as you like.
By the end, we've made our flash messaging system way more flexible and cleaner — giving us a nice, reusable system for user feedback throughout the app.