This episode is for members only

Level up with a premium membership

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

Episodes

0%
Your progress
  • Total: 4h 32m
  • Played: 0m
  • Remaining: 4h 32m
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.
50 episodes4 hrs 32 mins

Course overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.