‫All right.
‫So in the last video we made it so that we have to check to see if the listing owner is the one that's
‫logged in.
‫If it is, then we keep going.
‫If not, then we set an error message and redirect.
‫Now, I don't like setting the flash messages like this or like this.
‫So let's add a couple methods to our session class.
‫So we're going to oops we're going to go in here.
‫Why do I keep doing that?
‫We're going to go in here and go to the bottom.
‫And we're going to add a couple more methods here.
‫So let's.
‫Add a comment and this is going to set a flash message.
‫So as far as params, it's going to take in a string that is the key.
‫And then it's going to take in a pram.
‫That is a string.
‫That is the message to be set.
‫And it's going to return.
‫Void.
‫All right.
‫So.
‫It's going to be static as well.
‫So public static function and we'll call it set flash message.
‫And it's going to take in key.
‫Message.
‫And then all we're going to do here is call the set method.
‫Right.
‫We already have a set here which is this.
‫So just set whatever the key is to whatever the value is.
‫So we can call that with self since we're using static class.
‫Static methods.
‫So self and then set.
‫And then whatever the key is in the message.
‫Now, one thing I want to do is just prefix the key with flash underscore.
‫So I'm going to say flash underscore and then concatenate the key.
‫And then for the message, just pass in the message.
‫So simple as that.
‫That will set a flash message now to get one.
‫Let's say.
‫Get a flash message and unset because you want to unset it right away.
‫And let's add in.
‫Oops.
‫Let's say Peram.
‫It's going to take in the key of the message that we want.
‫And let's also add a param.
‫We'll say mixed and then the default.
‫And then return void.
‫So public function or public static?
‫And we're going to call this get.
‫Flash message.
‫Okay.
‫So that will take in a key.
‫And then whatever that whatever we want the default to be which is going to default to null.
‫So basically if it's doesn't exist then it'll be null.
‫So for the message that we're going to want to that we're going to get, remember it's always going
‫to be prefixed with this slash underscore.
‫We're not going to pass this in when we set the message we can pass in whatever success.
‫But then it will be saved as flash underscore success.
‫So then when we get it we have to account for that let's say self.
‫And then we'll use our Get method that we created.
‫And just make sure that we add in here the flash underscore.
‫And then whatever the key is and then.
‫Default, so that will get it.
‫Then we want to we want to unset it or clear it.
‫And we have a method.
‫We have a clear method.
‫So let's say self clear.
‫So we're just using the methods we've already created.
‫So clear.
‫And then remember the flash underscore.
‫And then whatever the key is.
‫And then we'll return the message.
‫So I actually don't want return void.
‫Let's say return mixed or return.
‫String.
‫And yeah, that should do it.
‫So let's save that.
‫Now when we set our messages, let's go back into the listing controller here.
‫We're now going to instead of doing this like you know session error message equals we'll do.
‫Session.
‫Set.
‫Flash message.
‫And then we can pass in this.
‫Or sorry.
‫We want to pass the the key in first which is going to be in this case error underscore message.
‫Okay.
‫So whatever whatever this is.
‫And then the actual message which will be this.
‫So from now on, that's how you'll set flash messages.
‫Not like this.
‫I think that's much neater.
‫And then we'll do the same here.
‫I'll just.
‫Grab that.
‫And let's change this to success message and then change the message to listing deleted successfully.
‫And then we can get rid of this.
‫And then we also have this in the store when we store listing.
‫I believe.
‫Is a store.
‫Yeah.
‫Or did I not add it here?
‫Oh, no, it looks like I didn't add it.
‫Okay, I did want to add it.
‫So let's go ahead and do that success message and we'll say listing created successfully.
‫All right now.
‫That should that should still work.
‫So let's try it out.
‫I'm going to just let's see.
‫Let's create a listing.
‫Just a dummy listing that I'm going to delete.
‫Okay, so it didn't show.
‫It didn't show the the message, but I think it's because on the create the create view, I don't think
‫we have the the partial.
‫Let's see if it works with delete.
‫So delete.
‫No, that didn't work either.
‫Um.
‫Because it should still even though in our partial which is errors.
‫Not errors message.
‫Right.
‫So it's checking for the success message here.
‫Oh I know why.
‫Because it's looking for this flash underscore success message.
‫Right.
‫So in this file we're going to change all this.
‫And I was going to do this after anyway.
‫But I figured this would still work.
‫But then I just remembered that we added that flash underscore.
‫So just to kind of test it out let's try this.
‫Flash underscore.
‫So that's for the success message.
‫Because even though here we pass in success message in the session class, right when we set it, we
‫put the flash underscore before it.
‫So let's try that.
‫I hope that works.
‫I'm going to feel like an idiot.
‫Test at Test.com.
‫And again for the.
‫There we go.
‫All right.
‫Good listening.
‫Created successfully if we delete.
‫Listing deleted successfully, so we know that it's getting set correctly.
‫Now we want to use this get flash message in our partial here instead.
‫Because again I don't want to use this this superglobal anywhere really.
‫So let's I'm just going to kind of rewrite this.
‫I'm going to go above here.
‫We do have to use this the namespace.
‫So up here we'll say PHP and let's say use framework slash session.
‫And I know it's kind of ugly because there's so many PHP tags, but what are you going to do?
‫And then I'm going to create a variable let's say success.
‫Message and set that to.
‫Session get.
‫Get flash message.
‫And then we want to pass in here success underscore message.
‫It's really being set as flash underscore success underscore message.
‫But we don't pass that in.
‫All right.
‫So yeah we'll end that tag here.
‫Then we're going to check for that.
‫So let's say PHP if.
‫Success message is not equal.
‫To null.
‫And then we'll end that.
‫So PHP and if.
‫Actually.
‫Yeah.
‫And then I want to put this.
‫Right here, this green div.
‫Except here I don't want to use this.
‫We're going to just output.
‫Whatever is in success message.
‫Okay.
‫So that that replaces this.
‫And it unsets in the get flash message as well.
‫Now for this one, we're pretty much going to do the same thing.
‫So I can copy that, except we're going to change all the successes here to error.
‫Sort of be error underscore message and then the color as well for the div.
‫So just grab that replace that green with that div.
‫And let's see if that works.
‫Okay, so that does look a bit neater.
‫So I'm going to just again create a dummy post.
‫Okay listing created successfully.
‫If I reload, that goes away.
‫So it unsets now if I delete it.
‫And there we go.
‫Now, if I try to delete one that doesn't exist, I get the error flash message.
‫Okay.
‫So just just to kind of recap, we created two methods to set the flask message.
‫And it's going to set it with the flash underscore.
‫It's going to get it passing in the key.
‫It's going to use that flash underscore before the key.
‫It's going to clear it and it's going to return it.
‫Okay.
‫So I think that that cleans things up a little bit and just let me just check.
‫We didn't use that anywhere else.
‫Did we.
‫Search for session.
‫Oh I did in the updated.
‫So we want to change that as well.
‫So let's copy.
‫Let's see.
‫We'll copy.
‫This.
‫And.
‫Replace this.
‫And we'll say.
‫Listing.
‫Updated.
‫Okay.
‫And that should be all of them.
‫All right, so that's it.
‫In the next video, let's make it so we can update because right now I can edit and I can update.
‫Like if I say graphic designer one save listing updated and it changed.
‫So I don't want to be able to do that because this isn't my listing.