‫All right guys.
‫So now we're going to implement something called flash messages.
‫And basically all a flash message is is when when you do something such as delete a listing, you set
‫a session variable.
‫And that session variable usually just has a message in it.
‫Like like listing deleted.
‫And then when you go to when you redirect, that message will show you want to show what is in that
‫session and whatever uses that key like success message or error message.
‫And then once it's shown you, you immediately unset it, okay.
‫So you show the message and then you unset it so that it doesn't continue to show.
‫So that's what we're going to do.
‫And we're going to start off just by directly using the session superglobal to set it and show it.
‫But later on we're actually going to have in our framework folder a session class to deal with all types
‫of stuff dealing with sessions, setting them, getting them, setting flash messages, etcetera.
‫All right.
‫So let's start off by going to our public index.php, which is our entry point.
‫Because in order to do anything with sessions, you do have to start a session.
‫You have to call session start.
‫And we went over all this back in the the learning modules if you watch the one on sessions.
‫So I'm actually going to delete the custom auto loader here.
‫So it'll be in past commits.
‫I just don't want it in the final code.
‫And I'm going to start the session at the very top here so that we can use sessions anywhere we want.
‫So let's say session start and then let's go to our destroy method in the listing controller.
‫Because that's where I want to show where I want to create the flash message to be shown.
‫And we're going to do that right before we redirect.
‫So let's say set flash message okay.
‫So the way we can do this is by using the session superglobal.
‫And then the key for this particular session variable is going to be success.
‫Six.
‫Success underscore message.
‫And the reason I'm doing success message and not just message is because I want to I'm going to have
‫one called error message as well.
‫And they're going to be displayed a little bit differently.
‫So let's set that to a string.
‫And we'll say listing deleted.
‫Successfully.
‫All right.
‫So we'll save that.
‫And right now this isn't going to do anything.
‫It's not going to show.
‫It's just setting that session variable.
‫Now we need to go into the view where we want to set it, which for us is going to be the listings page.
‫Right.
‫So this right here because that's where we're redirected to after we submit or after we delete a listing.
‫So let's go to views and then index.
‫Listings and then index dot view dot PHP.
‫And we're going to put this.
‫Let's see.
‫Let's go right under.
‫Um, actually, you know what?
‫One thing I want to change here on the listings is recent jobs.
‫This should actually say all jobs, because the recent jobs are on the home page.
‫Yeah, so that should say all jobs.
‫Then let's go directly under that.
‫So above the grid but below that, that all jobs.
‫And let's open up some PHP tags and we want to check for that session.
‫So let's say if.
‫Is set.
‫Session.
‫And success.
‫Underscore message.
‫And I forgot my question mark.
‫And then let's end the if so, whoops, what did I just do?
‫So let's say PHP.
‫And if.
‫And then inside that I'm going to have a div with the class of message and the class of BG dash green
‫dash 100.
‫And P-3.
‫And my dash three.
‫Okay, so this will style it, give it a green background.
‫Then we just want to output the session.
‫Variable, which is success.
‫Underscore message.
‫And that should show it.
‫Now I want to immediately unset it.
‫So I'm going to go under this div here and let's say PHP.
‫And we're going to call unset.
‫And we want to unset session underscore not underscore session brackets.
‫Success.
‫Underscore message.
‫Okay.
‫So it'll show it and then it will unset it.
‫It'll still show after we unset, but once we reload the page or go to a different page, it'll be gone.
‫So let's try it out.
‫Let's go.
‫Let's create just a dummy listing.
‫I'll just put whatever in here.
‫Okay, we'll save that.
‫All right, we come down here, we have our dummy listing.
‫Let's click on it.
‫Let's click delete.
‫And now we get redirected and we see listing deleted successfully.
‫Now if I reload the page it's gone because it got unset as soon as it got displayed.
‫So now we can create flash messages.
‫And like I said later on we'll have a specific session class with a method to do this rather than just
‫setting it, you know, just with with the Superglobal now.
‫I also want to have an error message one.
‫So we can basically just take this, copy it and paste it underneath with error message.
‫But that's starting to make the view kind of messy.
‫So I think that we should put this in a partial.
‫So let's go to partials and let's create one.
‫And we're going to call it message or yeah we'll call it message dot PHP.
‫And then let's take that.
‫So we'll cut this.
‫And we'll put that in here.
‫And then I'm going to go right under it and paste it in again, and then just change success here,
‫here and here to error.
‫And then change the color from BG green to BG red 100.
‫And then we can just include that partial.
‫So let's say.
‫What do we do in load?
‫Partial.
‫And let's load the message partial.
‫So that cleans up the view.
‫And we'll just try it once again.
‫Make sure that that still works.
‫Which it doesn't.
‫Let's see.
‫Hmm.
‫I'm loading ItrillionIGHT.
‫Right.
‫Load partial message.
‫Called it message.
‫You know what I think it might be?
‫Because I didn't reload.
‫Let's try it one more time.
‫Let's make sure it got deleted.
‫Yeah.
‫It did.
‫All right, so we'll try this one more time.
‫Just make sure you reload.
‫All right.
‫Save.
‫Okay, so now we'll go down to it.
‫Okay, so now that's deleted.
‫Let's delete this one.
‫Delete.
‫Reload.
‫All right.
‫Cool.
‫Not sure what happened there.
‫I think it was.
‫I think I just needed to reload.
‫All right.
‫So now we're able to show flash messages.
‫So what I want to do next is have the ability to edit a post.
‫And that's going to really be two phases.
‫So one is to create the edit form, fetch the listing, put it in the form.
‫And then the second is to actually do the update when we submit that form.