Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Map, Redirect and Named Routes4:28 with Alena Holligan
Map, Redirect and Named Routes
If I refresh the page, it has me resubmit the form. 0:01 Probably not what we want, so let's make this redirect to a thank you page instead. 0:04 We're going to add another route, so let's copy the first route, once again. 0:10 We'll change the first parameter to '/contact/thankyou'. 0:17 Now, we'll remove the logger and change about to thankyou. 0:24 Now, instead of rendering the thankyou after logging, let's redirect. 0:31 To redirect, we're going to change the response. 0:37 Return $response-> 0:42 withStatus(302)- to let the browser know that we're going to be redirected, and 0:48 then withHeader('Location', 0:54 as the first parameter and the URL, 1:00 '/contact/thankyou') as the second parameter. 1:05 Let's try this again. 1:12 Hello Ralph. 1:19 We receive our thank you message and 1:22 we can now see that we've been redirected to contact/thankyou. 1:25 Now when we refresh the page, it just refreshes this message. 1:30 It doesn't resubmit the form. 1:33 I know our routes aren't very complicated right now, but let's set up a named route. 1:36 Named routes make our routes easy to call within our code. 1:41 After our thank you route, we can add setName('/thankyou'). 1:45 Now before the redirect, we're going to set the URL, 1:53 $url = $this->router->pathFor('thankyou'). 2:00 Then we can use that URL instead of this location. 2:12 Let's go back and submit the form again. 2:18 And it still works. 2:22 Now if my simple validation doesn't pass I want to add an error telling the user that 2:24 all the fields are required. 2:29 After the conditional i'm going to add another item to my args array, 2:32 $args['error'] = "all fields required". 2:40 Now when I go back, and 2:50 submit a blank form, I see an error telling me that all fields are required. 2:52 We're going to be adding more to our form, but 2:57 before that let me show you how to combine both the post and the get routes. 3:00 Instead of using either get or post, we're going to use map. 3:07 The first argument for map is an array of 3:13 the HTTP methods that you want to match. 3:18 We'll use GET and POST. 3:22 Now some of this I only want to run on POST, so I'll add a conditional, 3:29 if($request->getMethod() 3:34 == "POST"), and 3:45 I'll surround all the code I want to run only on POST. 3:49 Let's check the form and make sure that it still works. 3:56 When I navigate to the page using get, I see the form. 4:00 When I submit a blank form, it works, and I see an error. 4:04 Great! 4:08 When I fill out the form, Hello Ralph, 4:10 I get my message, and I'm redirected to the Thank You page. 4:15 There's a lot more validation we can add, but I'm going to show you how we can use 4:20 Middleware to handle some of that validation for us. 4:25
You need to sign up for Treehouse in order to download course files.Sign up