Part 13 - Sessions & Flash Messages [How to Build a Blog with Laravel 5 Series]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to how to build a blog with level I'm your host J a Curtis and let's go ahead and keep moving along with this application we're building our blog we've learned how to create new posts which is great we've got those stored in the database now and before we move on to actually pulling them out of the database I want to kind of go back and review something we talked in the last video in part 12 we also had part 12 and a half so if any of you missed 12 and a half 12 and a half is an optional video what we did was set up JavaScript form validation it's not required for moving forward but it's just kind of a nice thing that maybe you want to learn we set up the parsley JavaScript it's called partially it's a JavaScript form validation library we set that up and learned how to use it and so now our forms have JavaScript validation in addition to what we did in part 12 which is the server-side validation okay when we were in part 12 we talked about how the the validation library in level is really really easy and if any of our validation on the server-side fails it'll automatically go back to the previous page and show all of the errors well I didn't really show how to actually display the errors it just kind of told you that it would so in this video I want to talk to you about sessions and flash missed flash messages as well as kind of managing those errors okay because what we did is we didn't actually set them up to display in our views even though labels kind of doing some of the backend stuff before us all right so let's go ahead and get started you all right so to get barred first thing we need to know before we move forward is about sessions now a session is a period of time when you're on a website okay so by default I know in level it's a hundred and twenty minutes which is two hours and basically the way level considers a session is anytime that you are in the browser with and you make a request within two hours of a previous one it's considered the same session if you make a longer period than if there's a longer you know period between such requests then two hours then it's considered a new session so sessions are really good because we can actually store things store data for our users in the sessions without needing to if it's like uncritically not critical information or things like that we can just pass it along in the sessions it gets deleted really easily it's not what you know permanently in our database or anything like that so sessions can be a good way for kind of information that's just very temporary in nature to be stored okay and that's what we're going to be doing with sessions in fact is I'm storing very temporary information things like messages because messages only need to be displayed very quickly and then they're gone and we can just delete them and putting them in a database can sometimes be overkill okay I'm not to say you can't do that you can actually store sessions and databases if you need to but it's kind of overkill for the what we're doing in this blog so let's go ahead and show you where you configure sessions before we you know get too deep into this in case you need to make changes to some of the settings on your sessions first thing we do is in your we're in the blog application so just we're opening up the project been working on and if you go under config there's a session dot PHP file down here now this is where you manage your session you don't manage the sessions but you can edit the settings for them so you can also see here that there's different ways to save sessions we can save them in the database we can save them as cookies or as files the default is files we're just going to go ahead and do that for now but you could obviously change them to cookies and databases if you need to I honestly think cookies are pretty good way to do it but a lot of people like to save them to databases the file is kind of like a combination of - it actually saves a physical file in our storage and then framework I think sessions and it'll actually store the sessions as a file in our like on the server so it's almost like having a database almost like a Redis kind of database but not like an O sequel database almost anyway that's basically what it is it's basically just an array that file it's in there so that's kind of what we're going to do by default but again you can work with cookies or anything else if you would rather do that you just change them and you can see your options are right here the other thing that we have here is the lifetime value of the session so by default it's two hours I think two hours is a very long session personally unless you were running something like a social network but for a blog I would honestly probably change this to 30 minutes for our purposes it's absolutely irrelevant but if you're working on you know more if you're actually working on an application for real life you might want to change that thirty minutes is like a good session for me I feel like if someone hasn't made a page request in 30 minutes they're probably it's probably basically a new session because they probably left and came back so I like 30 minutes as a session but we'll just keep it at the default of two hours so this is in minute so this is 120 minutes as two hours you can also expire on clothes I like to set this to true because to me with you close the browser or you close the window or whatever that your websites in the next time you open it should be considered a new session even if it was less than 30 minutes ago so I like to set true to this but this is mostly some of these settings I'm doing is because I'm so used to working with cookie sessions with file sessions they're kind of designed to be like a little longer lasting so you're not deleting and you know removing files all the time you can also encrypt cookies if you want this just uses your you have an encryption ID that's automatically generated when you did level new project so you can you can definitely encrypt these if you want um but I honestly don't think it's super needed it would only really encrypt cookies I don't even know if you why you would encrypt a file version I'm seeing if it lets you but anyway I'm not too worried about encryption you should really shouldn't have anything super important in your sessions the only time you might want to encrypt something is if you're saving it in a cookie and so the user would technically have access to change some information and you don't want them to be able to change it like for example changing their user ID so that they can bog in is someone else if you're if you're relying on that or something that would be bad so in that case you might want to encrypt it so that they can't change it but you really should be having in critical information in your sessions okay so that's basically how you would edit a session and remember sessions last in this case two hours is a session and then I want to talk about messages which are stored in the session variable because set a messages are very temporary in nature and so our sessions however within sessions there's a special kind of session called a flash session and a flash session is a session that only exists for the current request over the next request I'm sorry so for the very next request you can save a flash message and into those that only exists for one request and the next time you do the requests they're automatically removed and so those are really really easy to work with for messages because we only want them to exist and then be gone so that's kind of perfect for messages so that's what we'll be doing for messages so let's go ahead and open up our controller if we go over to app and pH HTTP and then open up our post controller I want to go ahead and add our first message in here under the store request same one we were working with before and basically I just want to if we successfully save the file if you know if the if we successfully save this in the database I want to be able to pass a successful message to the user so what we can do to make that happen is we can actually use something a class called session okay and with session you can put use session and flash and this creates a flash variable or a session that exists for the single request okay so it's a variable in our session it's saved in our session the flash just means only let it exists for one request and the next request just delete it okay so we're going to set that this takes two parameters the first parameter is a key and the second parameter is a value so the key is how we reference the session variable so this is what we're going to basically call it when we try to we try to reference it so in this case I'm going to go ahead and set it as success and then for the value this will actually be the message you want to output so we'll say the blog post was successfully saved and an exclamation point just like that so what we've done here this is how you can create messages so you just do session and then flash if you want a temporary one if you wanted a more permanent something permanently stored in the session at least for that 120 minutes you can do not forget you can do put and so with put in just basically we'll just add something but then this would be permanent based permanent in the session until the session is deleted or flushed so you can either views get a put or flash and I just like to flash because it's just temporary and it just goes for the one session and then it's gone which is what we need for messages okay so now we have the session but we don't quite yet actually there's something I forgot whenever we use these new classes especially these ones here that turned blue we need to make sure that we actually have them named spaced in in level five point two they're not in here by default okay so you need to bring them in so up at the top just like we've used all these other things we've named state space them in you can just want to use use a session and this will allow you to actually access that session variables so that you or a session class so that you could actually this will actually work for you if not it won't work you're just going to get an error okay so make sure you do that let me just focus go over that really fast so you've got our name spacing here we've been adding other things as we use them let's also make sure we bring in sessions so just use capital s and then session and that'll let us allow us to use session okay so that's all it takes now we're able to flash items to the session well the problem is now we got to pull it out of the session and display something to the user and that involves going over to our view so let's take a look at our view and set it up in our view now I'm going to go ahead and if you see what happens here is we're flashing this to the session and then we redirect to PO show which is this down here which we haven't set up yet and so let's go ahead real fast and just set this up very vaguely and so that we actually have a view that we can work with so this has a view when we try to redirect to that page so we're going to do return view and this shouldn't be surprised to you posts dot show all right and again that's just because this is the show requested in the post controller okay so then if we go over to our views let's go ahead and close all this up and we go to resources views and in posts we're going to create a new file because we haven't created this one yet and let's just save it as showed up blade PHP alright and then you should know how to do this we're going to go ahead and extend our main file our main template we can also since we're going to go ahead and setting this up let's go ahead and just set up the title as well so the title will just be equal to a vertical bar and then we'll just do a view post like that and then the next section is the content right and we'll just do n session section and then we'll just do a paragraph tag here just to keep it easy I'll give a class of lead it just kind of makes the text looks pretty and then we'll just say this is the blog post but it won't actually show the blog post we'll work on that in the next video okay I just want to have a page that we can go to show the to show the messages so what we need to do here is we're extending the main now we could obviously set some messages up in this view it would work just fine the problem is then we would have to do that to every single view and that's kind of a pain in the ass what we want to do is set it up in our template file so that we can just we automatically have access to our messages in any any view that inherits our template file ok so let's go ahead and set up main and if you open up main blade PHP you can see here we've got our head and then our navigation and then our content and what makes the most sense if you think a message it's usually like an alert message that's going to show at the top of the page but you don't usually want it kind of like below the navigation but before all the rest of the content so what we can do here is we've got a navigation here we open up a container and then we've got our content so let's go ahead just put it here at the top of the content so what we'll do is let's just create a new partial we'll do include include and then we'll do partials and then dot and we'll do on end to score messages okay and then now we've got to make a new partial for that so let's come into partials and we'll do new file we'll save this as underscore messages we do the blade PHP at the end all right let's go ahead and save that and now we can just put the code needed for our for our messages so what we obviously want to do is not every single that's going to be on every single page not every single page is going to have a success message right not every single one so we need to check to see if there is a success message to show and then we output the HTML if there is so what we got now obviously uses an if statement so I don't think I've showed you guys this before but we can actually create if statements inside of blade okay without having to actually make an eight PHP open tag and stuff like that so if you just use the @ sign and you do @ sign if this is almost identical to a PHP one but it just works inside of blade so with the @ sign if we can do our conditional and then it will we don't need to do the opening brackets like this we don't need that we can just do if and then you can do your content if that you want to display if the if the if statement is true and then we just end it with end if like that and now it'll check this conditional it'll output this if you know the conditionals true and then it'll just that's the end of the if statement and it continues on okay so what we want to do here for conditional is we want to check to see if something is in the session variable we would do session and you might think oh let's do like maybe is set something like this is set session you know and check about check the flash message but there's actually a shortcut we can do to see if something is in it our flash we can do session has and what this will do is you just do has and then the name that you're looking for so in this case we called it success I want to show you again come back over here we called this success and that's stored in our session ok I'm a lot of people get confused with the flash and seem to think it's like called flash success and the most common error I see when I teach people is they'll type flash in here and this will always cause problems because flash is permanently there's always an item in your session called flash and it's it's managed by label so this is always going to come back true regardless of whether there is something or isn't something and it'll just cause havoc and it's never going to output the message you're looking for because the message is not called flash the message is called success okay so don't get that confused what we want to do here is called is success okay and that's that this right here is the name that you're looking for all that flash means is only is to only keep it for one very one request that's all that flash means okay but success is the name and it's stored in session so when we come over here we just want to see does set does the session have success okay if it does it'll return true which means we'll go into our if statement so in here what we want to do is output our HTML now since we're using bootstrap there's already alerts created for bootstrap so let's take a look at okay so um as you can see here if you come up to I was already kind of there I was taking a look at it before but if you're on your get bootstrap comm and I'll have a link for this in the description as always guys I'm looking out for you but I'm you go to get bootstrap comm and over to components and actually I'll probably just linked directly to alerts but you go to components and then down over here on the side we have alerts and these are what the alerts look like so this is a successful one is this green one and this is what we need to do we just create a div and give it a class of alert and alert success so it's pretty actually pretty easy let's go and just copy this though this top one for success and copied over and paste it in here and then let's just clean this up so we get rid of these dots and make a new line and now for the body of our success message we want to do I'm going to go ahead make a strong tag and say success like this and then actually let's not do what we don't need a space because we'll do a space over here let's just do a space and then we'll go ahead and do our double brackets for blade so we can output the knee or the value of our session which will be the actual message so we do session and then get and this gets it out of the session and then the name of the item in the session because remember we can have tons of different things in the ship stored in the session so this allows us to do success and that will pull the success message out of the session so that's all we needs go ahead and save that and let's go actually give it a try let's make sure this works now you might need to depending o there's actually something I just remembered that we need to do before we move forward we actually need to go to our routes because we're not taking advantage of sessions currently in our routes let me show you and I mean if you go up to app and then down to HTTP and then routes all right you can see that there's this mystery little law it says application routes right here and you can see this is a group called middleware we haven't talked about middleware yet but middleware is basically what it's basically code that runs after the the routes after something has sent to the route but before it gets to the controller so right here what we do is if someone makes a request to say the contact page and it goes immediately to the routes file finds the contact page and then goes immediately to the page controller middleware is something that sits in the middle and what we can do is if we have middleware it'll actually process all the code in the middleware before going into the controller this is really good if you have things like authentication or something like that that needs to be set up for every single request and you don't want to replicate it in the controller that's when middleware becomes really really useful now there's a middleware that's called web and web includes things it even tells you up here it includes things defined in your HTTP kernel and includes session state CSRF protection and more so we session state and so we actually have to make sure that our routes are inside of this this Web middleware and before we work with sessions now we want sessions to be in everything so we're just going to ahead and take all the routes and put them into this middleware so let's just do that so let's just copy all of this we'll cut and then make sure you get really comment here and paste okay so now all of the sessions here well hat will go through the middleware the web middleware and then have access to things like session okay so now we should be ready to set up so let's go back over to our our blog and then let's go to posts slash create okay in the next video maybe well we got a we got to actually add a button for this right now we don't have it in our navigation but let's go to post slash create like you're going to create a blog post we're going to submit the blog post which we did in the last video it should be successful and if it's successful we should get redirected to the show page but this time we should get last time we got a blank screen this time we should get a success message so let's go ahead and do it let's say testing our notifications and then for the body we'll just say this is a test of the notifications service we just started like that we'll go and click create post okay great we got it was all successful and you can see that we got a message right here okay so it says success the blog post was successfully saved and that's the message that we passed in to our into our session and then we pulled it out of the session and put it into the view alright now if we refresh this page what's going to happen is it's going to be a new request so that session variable since it was flash is going to be deleted and we should not get the message anymore you should only show the blog post let's try it let's just refresh and sure enough we refreshed we get the blog post it's not the real blog post but it's our dummy one and then we're still on post slash 12 which is good you'll probably on post 3 I do a lot of work in between the videos and so I can end up creating a lot of posts so I am up to 12 now but you'll be on probably number three and then we have the message anymore so this is really good the page loads even if there's nothing in our set session variable and that's awesome that's really really good just like that well now you might be wondering what if there are error messages all right well how do we handle error messages let me show you okay so now we know how to get we've got our success message set up but we don't have our errors message set up and I mentioned how in our post controller this validate will automatically submit errors back to our to the same request so if this fails here or here what happens is it goes back to the previous page or just create and it will automatically add a message called errors to the flash session okay so it'll automatically flash it to the session which is exactly what we did with success but now I want you to show you how to work with the automatic errors that might be generated from label now when this happens and there's errors created label will automatically flash to the session something called errors okay and that will include an object with multiple errors inside so what we want to do is if we go back over to our messages let's go ahead and get that set up in here what we can do is we can check to see if there are any if there are any errors to show now we're going to do it a little bit differently this time instead of doing to see if session has errors there's always going it's always going to come back true because label actually sets that even if there are no errors it automatically sets it when we go through our web middleware that we set up just earlier and so it's always going to come back true in this case which is different than when we handle it ourselves because this only comes like true when we actually set it otherwise it'll come back false so what we're going to do is it there's always good to be something called errors but usually it'll be set to there only be there will be zero items in the object but if there's errors to display they'll be one two three however many errors there are what we're going to do is we're going to see if count of the errors object is greater than zero okay so if there's more than zero objects in there if there's any objects then we would enter this if statement okay let's go ahead we'll just end the M and if and then the middle here what we want to do is we're going to go ahead and create our the Dib just like we did here so let's just we can just we'll just make it real fast so it's going to be div with a class of alert and then alert danger which gives makes it the red color in with bootstrap all right and then just adjust for screen reading you do this roll alert it actually doesn't affect anything other than for screen readers close the div and then the middle I just want to say strong and we'll just do errors like this and then under here what we're going to do is loop through all the errors that we might have because we don't know if there's one two three however many there are so what we're going to do is create a loop now we can create loops in blade the same way we do if statements with just the @ sign so I'm going to go ahead and start a loop we're going to do a for each and then we're going to do for each errors as error like that okay so we're going to pull out each item in the errors object and call it error and then let's just go ahead and end for each here and that's how you would create a for each loop and then in the middle here all we need to do is output the the actual error message so well what we're going to do here let's do error that'll actually output the message and then let's just go ahead and wrap this in list item tags so list item clothes list item we probably should here then call it a an ordered list and then at the end of the 4h will close the unordered list let's clean this up a little bit like that okay so we're going to create an under our list and then display every error in there so that should create our errors I hope that wasn't too fast for you guys we just created the div here um this is just to show the text for errors we create an unordered list then we loop through every error in this errors object call the error submitted it there and wrapped it in Li tags so that it'll just be a bulleted list of all the errors all right and this we'll only show if there's more than zero errors let's go ahead and save that and come back over to our web page and test it so if we come over here let's go ahead and just try to submit it without anything filled well it's just refresh it make sure that we've taken everything into effect and then we'll um submit it without any errors or without any anything filled in okay well we have one problem here our JavaScript form validation that we set up in the previous video is going to catch all these errors I can actually submit it because of the JavaScript validation so we could turn off our JavaScript but what we'll just do here is remove the JavaScript perform validation really fast and then we'll add it back in when we're done testing so let's just come back over to our create and then in our form we'll just get rid of this data partially validate so that it doesn't try to validate and then let's also get rid of the required and Max link and let's get rid of this required like that okay make sure you keep the parentheses in good order this way we're not going to get the JavaScript validation let's refresh okay now when we submit it we shouldn't get JavaScript validation perfect now this time when it is actually submitted our errors it looks like we have an error with how we have a problem with how if we output the errors let me go ahead and take a look at the code and see what the bug might be okay here's our errors okay so I think so what happened here is errors we actually need to call an OP a method on this says all okay so we need to make sure we were actually pulling out all of the errors and then call each one as error that minor thing is what we need to fix let's go and save that and refresh this so we fetched it because it's flash the message goes away let's submit it again okay and now you can see that we actually got the errors so the title field is required and the body field is required now let's go ahead and let's just type in a whole bunch of characters here and I want to get over 255 characters and see if that one works so 255 characters is a ton guys like a ton okay so that should be over 255 though yeah there should be opportunity five let's go ahead and create post there we go so the title may not be greater than 255 characters and the body field is required so here we are we've got our error messages and now we're ready to continue on because now we're going to be able to submit those errors and are those success and error messages as we move throughout our application alright now the last thing we need to do before we move on into the next video is just undo some of the JavaScript remember when we got rid of the JavaScript perform validation let's just go back and undo that to bring it back to our application because we're going to need that going forward so if we go back over to the create blade PHP you should just be able to do ctrl Z or Apple Z and just undo everything we did here save that and now we should have our JavaScript form validation back okay which is what we need all right so I'll see you guys in the next video we're going to work on getting we're now going to pull items from the database and show them on our show page and also in our index page right
Info
Channel: DevMarketer
Views: 56,615
Rating: undefined out of 5
Keywords: laravel, code, tutorial, session, flash, messages, php, framework, easy
Id: -FMecyZs5Cg
Channel Id: undefined
Length: 29min 45sec (1785 seconds)
Published: Thu Feb 18 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.