Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
As a little bonus, letβs add some nice messages in the session flashes to give feedback to the user.
Styles for New Function display_success()
Name of bag: success
Name of class: alert-success
$messages = $session->getFlashBag()->get('success');
$response = '<div class="alert alert-success alert-dismissable">';
Success and Failure Flash Messages
- Adding Book
- Editing Book
- Logging In
- Logging Out
- Registration
Display Flash Messages on Pages
- add.php
- books.php
- edit.php
- index.php
- login.php
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Up until now when there's been an error,
0:00
we just redirect the user back to
a form or page that they were just on.
0:03
But we do not provide any information.
0:07
I think we should fix this and
show some sort of message.
0:10
When a message is only visible for
0:15
a single request,
this is called a flash message.
0:17
The Symfony HttpFoundation
package that we've been using
0:21
gives us a nice set of
features to do this.
0:25
We'll be using Sessions for this, and
attaching a message to its flashBag,
0:28
the storage area for all these messages.
0:32
The messages that are stored
here are automatically deleted
0:36
when you retrieve them
to display on the site.
0:40
The first thing we need to do
0:43
is open our Bootstrap file and
0:48
begin a session for our application.
0:53
This will create a new session object and
start it.
1:11
This is essentially the same
thing as doing session_start.
1:15
That gives us a lot more
features to work with.
1:19
To show how this works,
let's take a look at our login system.
1:22
Open the do login procedure and
let's add some items to our session.
1:26
If a user's email is not found, we should
let them know that the username was not
1:32
found when we send them
back to the login page.
1:37
We need to add a line to this check for
the empty username.
1:40
From the session object in the Bootstrap
file, we get the FlashBag and
1:48
can add to it.
1:53
The first item is the key we want to use,
in this case, error.
2:00
And the second is the message,
Username was not found.
2:06
Internally, it will create an error array
2:17
with all the messages
that belong to that key.
2:20
To display the message, we need to check
for anything in the error FlashBag.
2:24
I think this calls for
a nice helper function.
2:29
We'll call this display_errors.
2:38
And first we'll need to get
the global session variable.
2:46
After that,
2:56
we check to see
3:01
if the FlashBag
3:06
has an error.
3:11
If not, we can return.
3:16
If so,
3:21
we need to get
3:24
the errors.
3:29
Now we can create an alert
with all the error messages.
3:38
Let's loop over
4:14
our messages.
4:21
And finally, return the response.
4:38
Now we need to display
errors on our login page.
4:45
Let's take a look at this in the browser.
4:59
If any errors exist,
they will now show here.
5:09
We can expand this to show success
messages and other kinds of messages.
5:13
Why don't you give it a shot and
add the function to display success and
5:18
add these messages where
you find appropriate?
5:22
Check the notes associated with
this video for style examples.
5:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up