1 00:00:00,690 --> 00:00:03,010 Up until now when there's been an error, 2 00:00:03,010 --> 00:00:07,660 we just redirect the user back to a form or page that they were just on. 3 00:00:07,660 --> 00:00:10,890 But we do not provide any information. 4 00:00:10,890 --> 00:00:15,100 I think we should fix this and show some sort of message. 5 00:00:15,100 --> 00:00:17,140 When a message is only visible for 6 00:00:17,140 --> 00:00:21,680 a single request, this is called a flash message. 7 00:00:21,680 --> 00:00:25,540 The Symfony HttpFoundation package that we've been using 8 00:00:25,540 --> 00:00:28,430 gives us a nice set of features to do this. 9 00:00:28,430 --> 00:00:32,740 We'll be using Sessions for this, and attaching a message to its flashBag, 10 00:00:32,740 --> 00:00:36,930 the storage area for all these messages. 11 00:00:36,930 --> 00:00:40,460 The messages that are stored here are automatically deleted 12 00:00:40,460 --> 00:00:43,230 when you retrieve them to display on the site. 13 00:00:43,230 --> 00:00:48,260 The first thing we need to do 14 00:00:48,260 --> 00:00:53,728 is open our Bootstrap file and 15 00:00:53,728 --> 00:01:00,739 begin a session for our application. 16 00:01:11,066 --> 00:01:15,050 This will create a new session object and start it. 17 00:01:15,050 --> 00:01:19,310 This is essentially the same thing as doing session_start. 18 00:01:19,310 --> 00:01:22,060 That gives us a lot more features to work with. 19 00:01:22,060 --> 00:01:26,260 To show how this works, let's take a look at our login system. 20 00:01:26,260 --> 00:01:30,010 Open the do login procedure and let's add some items to our session. 21 00:01:32,720 --> 00:01:37,120 If a user's email is not found, we should let them know that the username was not 22 00:01:37,120 --> 00:01:40,470 found when we send them back to the login page. 23 00:01:40,470 --> 00:01:43,840 We need to add a line to this check for the empty username. 24 00:01:48,857 --> 00:01:53,429 From the session object in the Bootstrap file, we get the FlashBag and 25 00:01:53,429 --> 00:01:54,370 can add to it. 26 00:02:00,968 --> 00:02:06,495 The first item is the key we want to use, in this case, error. 27 00:02:06,495 --> 00:02:13,820 And the second is the message, Username was not found. 28 00:02:17,790 --> 00:02:20,840 Internally, it will create an error array 29 00:02:20,840 --> 00:02:24,250 with all the messages that belong to that key. 30 00:02:24,250 --> 00:02:28,695 To display the message, we need to check for anything in the error FlashBag. 31 00:02:29,720 --> 00:02:31,680 I think this calls for a nice helper function. 32 00:02:38,750 --> 00:02:42,200 We'll call this display_errors. 33 00:02:46,808 --> 00:02:50,480 And first we'll need to get the global session variable. 34 00:02:56,630 --> 00:03:01,256 After that, 35 00:03:01,256 --> 00:03:06,302 we check to see 36 00:03:06,302 --> 00:03:11,768 if the FlashBag 37 00:03:11,768 --> 00:03:16,819 has an error. 38 00:03:16,819 --> 00:03:18,430 If not, we can return. 39 00:03:21,600 --> 00:03:24,351 If so, 40 00:03:24,351 --> 00:03:29,396 we need to get 41 00:03:29,396 --> 00:03:34,451 the errors. 42 00:03:38,577 --> 00:03:42,205 Now we can create an alert with all the error messages. 43 00:04:14,351 --> 00:04:21,324 Let's loop over 44 00:04:21,324 --> 00:04:27,802 our messages. 45 00:04:38,108 --> 00:04:40,180 And finally, return the response. 46 00:04:45,977 --> 00:04:49,212 Now we need to display errors on our login page. 47 00:04:59,879 --> 00:05:01,900 Let's take a look at this in the browser. 48 00:05:09,748 --> 00:05:13,600 If any errors exist, they will now show here. 49 00:05:13,600 --> 00:05:18,540 We can expand this to show success messages and other kinds of messages. 50 00:05:18,540 --> 00:05:22,450 Why don't you give it a shot and add the function to display success and 51 00:05:22,450 --> 00:05:25,320 add these messages where you find appropriate? 52 00:05:25,320 --> 00:05:28,840 Check the notes associated with this video for style examples.