This episode is for members only

Level up with a premium membership

Join now
Already a member? Sign in to continue
Playing
37. Basic session flashing

Episodes

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

Transcript

00:00
Let's now look at how we manage our session within our framework. And this serves quite a few purposes. The main one that we're going to cover is to flash messages to the user.
00:10
Basically, what that means is when we do some sort of action like sign out, we can flash a temporary message to show to the user you have been signed out. And then when they refresh the page, the message at the top will disappear. The second way that this is useful is for validation.
00:27
So for example, if I go ahead and log in and hit this without entering any fields, we want to show validation messages underneath here. But then when we refresh the page, we want these to disappear. So we'll be using that in conjunction with the validation we'll implement very shortly
00:42
to show a message to the user with a validation error. The other use of this is if you just want to store something in a session at any point while you're using the framework, you can just add something to the session and then retrieve it later.
00:57
OK, let's take a look at how we get this set up. Now, we don't need to install any packages for this because we already using some symphony components. We already get the symphony HTTP foundation package pulled in.
01:09
That means that we have access to a session class just over here. So we're going to add this to our container, which means that we can extract it anywhere we need, add a message to our session or add a key value pair to our session
01:24
and retrieve it from anywhere in our application. OK, let's go ahead and create out a service provider for this. So once again, we'll just grab our app service provider and let's call this session service provider.
01:36
Let's go ahead and paste in the contents of this and do what we've been doing. Change over the name, get rid of boot and let's go ahead and register this. So over to config an app and we'll pull in our session service provider just in here. OK, so over in our session service provider,
01:54
like I said, this is already pulled in. So let's go ahead and grab our container. Let's add to the container using that session class we just had a look at. So this is under the HTTP foundation session namespace.
02:08
And let's go ahead and just return a new instance of this session in here. That is pretty much all we need to do to get this working. Let's go ahead and set this as shared and we are good to go. OK, let's look at a really basic usage of this.
02:25
And then in the next episode, we'll build out a Twig helper so we can flash a global message in our app layout. OK, so to do this, let's go over to our logout controller. And once the user has signed out, let's flash a message in here.
02:39
So to do this, we're going to bring in session out of our container. Make sure we put in the right namespace for this. And let's look at how we add something to our session. So let's say this session.
02:52
And from this, we want to get the flashback and we want to add an item to this. So this gives us a type and a message, so effectively just a key pair. So let's go ahead and flash something called message. And just say you have been logged out.
03:10
OK, so we can pick this message up by accessing the flashback, but using the get method anywhere we need. Let's look at the long way that we would do this. And then in the next episode, we'll add a helper.
03:23
OK, so that is being flashed now. Let's go ahead and just try and sign out just so we can test that this is working. So let's hit logout. And yeah, that will have been flashed to the session.
03:33
Let's pick this up on the home page by passing the value down to the controller. OK, let's go over to our home controller. And again, we'll pull in our session just at the top here. And let's go and access this value.
03:46
So for now, let's just dump on this value just so we know that we can get it out. And then we'll pass it down to our view. So let's use the dump function, which again comes from symphony. And let's say this session get flashback.
03:59
And let's say get message. So we're basically just putting it into the session and then retrieving it. OK, let's go over to login. Let's sign ourselves in.
04:10
And let's go and log out. OK, there we go. So you can see that this value has been flashed. It looks like it's been flashed twice, which is not the end of the world.
04:18
It might just be the way that we're doing things. But we'll double check this in just a second. So we know that we can get that value out now. OK, so if we were going the long way around and we weren't building out a twig helper,
04:29
and you can do this. There's nothing wrong with this. We would pass this directly down to the view. So what we would do is we would grab this value.
04:37
And then we would just grab the first item of that value. And that would give us the message. OK, so now that we've got this, we can just pick this up on our home page. So we can just add an if statement in here.
04:49
So if message is defined. And actually, let's think about the way that we're doing this. So we're getting the message. We could just do this within the template.
05:00
So we'll just use that to say is the overall message defined. And then if the message is defined, then we'll output message and zero directly inside of here. So we'll just do it this way rather than pass the first key down to the template, because otherwise we'll end up with a error in our home controller.
05:20
OK, let's go over and let's sign in. And let's go ahead and log out, and we should be redirected over to the home page. And as you can see, the message is now displayed. Because we're using a flash message when I refresh now,
05:34
that will have already been gotten rid of the session after it was displayed to us. So then it disappears. OK, so now that we've done this, let's go over and build a helper so we don't have to manually pass all this stuff down to our view.
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.