Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introducing MVC Frameworks in PHP!
You have completed Introducing MVC Frameworks in PHP!
Preview
Routes
This video doesn't have any notes.
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
When we view our site in a browser, and
0:00
navigate to the Contact page,
the highlight on our navigation changes.
0:02
But it still shows our About content.
0:07
Let's take a look at our routes.php file.
0:10
The first parameter of our git method
0:14
has the name variable
surrounded by square brackets.
0:16
This means,
that the argument is optional, so
0:21
this route will match with or
without the contact.
0:24
I want to set up a new route for
my contact page.
0:29
So I'm going to start by
duplicating this route.
0:32
I need my contact route to
come before the about route.
0:38
Since we saw that the about
route with also match contact.
0:42
Let's change the first
parameter to be /contact.
0:46
Then I'm going to remove the logger, and
0:56
change about to contact-form.
1:00
Now when I visit contact in the browser,
I see the form.
1:07
If I try to submit the contact form,
it gives me an error, method not allowed.
1:11
The only method allowed is get,
let's add a post method.
1:17
I'm going to copy my contact route and
then change the method.
1:23
Instead of get, I'm going use post.
1:31
Now when I post the form,
I see the contact page again.
1:36
When I fill out the form,
nothing new happens.
1:41
I go back to a blank form.
1:46
The first thing I want to do,
is have the form save my values.
1:49
If I open the contact-form.phtml,
I can see that my values here
1:54
are set to use the name,
email and message variables.
2:01
I just need to tell my view
about those variables.
2:07
In my routes file, we can see that
the third parameter is the args array.
2:12
I need to get my post
values into my args array.
2:19
The post details are stored in request.
2:26
GetParsedBody.
2:32
So I need to merge this array with any
other args passed into this route.
2:36
args = array merge (args).
2:42
And my parse body.
2:51
Now when I post the form,
I keep any values I may have entered.
2:57
Next, I'm going to do
some simple validation.
3:02
If the name and email and message are set.
3:07
I want to log the details,
and show the thank you page.
3:12
So, if not, (!empty($args, [name]).
3:16
And not empty ($args [email]),
3:29
and not empty ($args [msg]).
3:35
I'm going to log these details as json,
so $mail = json_encode,
3:44
($args['name']), $args['email'].
3:53
And args msg.
4:03
Next, I'll copy the logger
from our first route.
4:09
I'll change info to notice.
4:19
And then I'll pass
the json_encoded mail variable.
4:23
Now I'm ready to show the thank you page,
4:29
return $this-> view-> render($response,
4:34
'thankyou.phtml' and the $args).
4:41
Let's fill out the form and submit.
4:48
My Message.
4:53
Now we see a thank you message,
and let's check our logs.
4:55
Okay, our message is logged as a notice.
5:05
You probably won't just log your emails,
but
5:10
since Emerson isn't checking emails
anyway, it doesn't really matter.
5:13
You can always set up a mailer
dependency to send mail.
5:18
Check the notes for more details.
5:22
Let's check out some more
routing functionality.
5:24
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