This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
38. Creating a Twig helper and flashing globally

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
We've already built quite a few Twig helpers.
00:02
So feel free to do this on your own, if you want to give it a try. Otherwise let's get rid of the message that we're passing down to our home controller and let's get rid of the session in here because we're not going to need that now.
00:13
Okay. So if we head over to our Twig runtime extension, once again, we're just going to create out another method in here, which returns to us the entire session object. Let's create that out and we'll access our session from HTTP foundation and session.
00:28
Once again, just make sure that's pulled in at the top. Okay. If we head over to our Twig extension here, let's register out a new Twig function here called session.
00:38
And now what we can do is just access this directly in our template. So we at the moment are doing this over in home. Let's get rid of this and we'll need to modify this slightly. Let's create out a global flash message in our app.twig template.
00:54
So we could just do this at the very top. And of course you can style this out as well. If you want to, let's have a look at what we need to do here. So we'll access our session and we'll use get flashback in here directly.
01:07
Then we will say get, and we'll check that message. So we'll do exactly what we did in our controller, but directly within the template. Then what we could do is grab this value here, output this with the
01:21
first item and that's what we need to do. Okay. Let's try it out. So I'm going to go ahead and log in again and let's go and log out.
01:28
And there we go. We have a flash message at the very top of the page. Of course, even when we're redirecting, cause that will be set displayed. And when we refresh that disappears.

Episode summary

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.

Episode discussion

No comments, yet. Be the first!