7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 13Length: 1.4 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.4 Create Route Parameters

While it's useful to create simple static routes within your website or app, you will often need to create more dynamic routes that can take parameters. In this lesson, I'll show you how to modify your existing static route to make it more dynamic and take parameters. You'll also see how to make those parameters optional.

2.4 Create Route Parameters

Now we'd like to spend a little bit of time customizing our route here to handle some input so that I can display a little bit more of a customized greeting to whoever is coming to this particular page. Now what we would want to happen here, in my mind, is within my route to my page here, I'd like to specify somebody's name. So maybe put my name in there, and go ahead and execute that. Now obviously I haven't defined that specific route within my application, so, I get a Page not found. But now I want to be able to handle this specific this scenario. So how do we do that? Well, it's going to take two different pieces. The first thing that I need to do is I need to come back to my route, and I need to modify this a little bit. I wanna modify this so that I can come in here and I can specify the name of a person that I can greet. And the way that we do that is we specify open and closed curly brackets, and then we give it a name. So I'm gonna say name is going to be what I'm going to refer to as the parameter within my URL. So now I can save that. I'm gonna come back over to my calculator controller, and now I need to take in that particular variable into my hello function. And the way that we do that is we say this is going to be a string, and I want this to be $name. So that's going to be the variable that I can refer to throughout this particular function to handle that name. So now, let's modify our markup a little bit so that it's a little bit more interesting to the end user. So what I'd like to do this time is, I would like to use a special function in PHP called sprintf which allows me to do some string formatting. So let's grab this string, and we'll stick it in here. Now, in this case I want to say Hello there, and I want to do a comma, and I can inject any sort of valid HTML in here. So I'm gonna say strong because I want this to be bold. So I'll do strong, open and close. And then within here I can specify %s, which means I want to inject the next parameter to sprintf into this location of this formatted string. And so all I need to do then is specify what I want to inject in there, and I'm going to inject the $name. So let's go ahead and save that. And I want to come back over to performance. Let's go ahead and clear our caches just to be on the safe side. And once that's done, let's come over here and refresh this page. And I should get Greetings. Hello there, Derek. Which is really, really cool. So now, I have the ability of customizing the URL path to take in parameters that I can handle within the controller function that I defined within my routing file. But we've now kind of changed things a little bit. So what happens if I come back here and I try to go back to the original one? Well, now that route doesn't exist anymore, and that does seem to be a bit of a problem. Maybe a user has gone to this site that, or to this location, to this route, that maybe hasn't logged in yet, or maybe there wasn't a parameter passed in. And that's really not a reason for us to throw up a Page not found. We should try to handle that a little bit more gracefully. So in that case what we probably would wanna do is just not worry about the name. Maybe just say hello there, or something like that. So in order to handle that we can specify what's known as an optional parameter. So in order to do that, I can come back into my routing file here, and I can now change it so that this name is optional. And the way that you do that is by coming into your defaults and giving the name of that parameter. In this case it's name, and give it a default value. So, let's just say Derek is gonna be the default value. So let's go ahead and save that. Once again, we'll come over here, and we'll clear our cache. And then we can come back in here. And if I were to refresh this page now, I should get, Hello there, Derek. So anybody, or any time that we come into this URL but it doesn't exist with a parameter of a name of a person I'm trying to greet, it's gonna default to Derek. So then I could do /John and I could go ahead and execute that. And as you can see now, I get Hello there, John. So now you've seen how you can do a couple of different things. How you can inject parameters into your routes so that your controllers and functions can handle those things to do sort of customized logic that's not always the same, that's more driven by the end user. And you might be saying to yourself ,well, why would I care about something like that? Well in a lot of situations you would care about that Ii you were handling user submitted information. Now, in this case, I'm really not. All I'm handling in here is some munging of the URL, which is great to play around with, but it's not very user, it's not a very good user experience. So what I'd like to do is get to a point where I could maybe display a form to the end user with some text boxes, maybe a drop-down, a Submit button, things like that, so I can get some input from the end user. They can hit the Submit button. They can send it to my application via my routes, and then I can handle those requests in a very customized way, and then handle the output appropriately. And that's exactly what we're gonna start getting into over the next couple of lessons.

Back to the top