This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
54. Route generator helper

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
Let's create one more helper function to resolve routes for us.
00:03
Now, this is really important because over in our navigation at the moment, we're referencing things just by name. We're also doing that inside of things like our login controller. When we redirect the user back somewhere or redirect them to their final destination.
00:18
Now, it's very rare for route URLs to change in an application, but it's best practice to reference these by name rather than by URL. Now, by that, I mean, if we head over to our web routes, we could pick any of our routes here.
00:32
And we're just going to look at a couple of examples here. We can set a name on these so I could just call this home. Now, that's never going to change, even if the homepage URL does change. So now that we've set a name on a route, we can create our helper function
00:47
to resolve the path of this by its name. And then inside of, say, our navigation, rather than reference this as slash, we could do something like route and home. Now, obviously, that's not going to work at the moment.
01:02
We don't have a function called route. Let's go ahead and do this now. OK, so the first thing that we're going to do is head over to our helpers and we'll create another function to deal with this, not a public function.
01:13
And we're just going to call this route. Now, what do we need in order to resolve a route? Well, we do need a name, obviously, but we also potentially need an array of arguments.
01:26
Now, remember a little bit earlier, we built out the route that showed a user's details. So this one here. So this is an argument that would need to be used to resolve this route if we gave it a name.
01:37
So let's actually give this route a name as well. So we've got a couple of examples. So we're going to call this users and show. So users dot show.
01:46
OK, so back over to our helpers. We know our arguments are now. Let's go ahead and grab our router from our container. So remember, that comes from league route and we're going to use get named route from here.
02:02
And the name is exactly what you would expect. It's just the name we've passed in. And then we want to get the path from this. So this will give us back an object.
02:11
But we want the path for that route that's represented as an object. And we can pass the arguments into this to resolve that. OK, let's go ahead and try this out. So we need a tweak function for this because we're trying to use these directly in our templates.
02:26
So we're going to need to build out a tweak function as well. So let's go and just take any of these. We'll do this at the bottom here. And let's create out that route helper again, we're going to take in the same signature, so it will be the name and an array.
02:44
Of arguments, and we can just use our helper function in here so we can just return route name and arguments, and that should give us what we need. Let's go over and give this a refresh. And yeah, we've got to register that over in our tweak extension.
03:00
So let's do that now. And we should be good. OK, let's call that route and we'll head over and give that a refresh. OK, so it looks like this isn't working.
03:09
Let's go back and just review what we're doing inside of our helper. And yeah, this needs to be router, not route. So let's pull in the correct class in there. Give that a refresh.
03:20
And there we go. I can click through to the homepage and that now gives me the URL for the homepage. Now, over on the homepage, remember, we are clicking through to see a user. Let's try this new route helper with this in here and let's see how this works.
03:35
OK, so let's go over to our homepage and I'll let you go ahead and fill in any of the other links that we're using using this new route helper. I'm not going to go through every single one. OK, so we want to link through. Instead of this, we want to use our route helper to go through to users show.
03:56
And then we need to pass an argument directly into here to do this within Twig. It's a little bit different. We're not going to pass through an array with user and then the user's ID. What we need to represent this as is like a JSON object.
04:10
So we're going to say user. And then in here we know each of the users in the loop. So we're just going to pass the ID directly through. That will be passed as an array to this function and then it will resolve it with the correct ID in that argument.
04:27
So let's click on Alex. And yeah, you can see that works. Let's go to the second page, click Mabel. And yeah, that works as well.

Episode summary

In this episode, we're cleaning up how we generate URLs in our app by creating a handy route helper function! Right now, we reference routes by their hardcoded URLs all over the place—in our navigation, controllers, and redirects. That's not ideal, because if a route ever changes (even if it's rare), we'd have to hunt down all those places and update the URLs manually.

Instead, it's way better to reference our routes by their names. We start off by setting names on our routes in the web routes file. For example, we name the home route "home" and a user details route as "users.show". Now, these names stay the same even if the URLs themselves change down the line.

Next, we build our helper function called route. This takes the route name and an optional array of arguments (like user IDs) and uses our router to resolve the correct URL path dynamically. We also wire this up as a Twig function so we can use it easily in our templates.

We then update links in our navigation and homepage templates to use this new route helper instead of hardcoded paths. For routes needing arguments, like user profile pages, we show how to pass those values directly in Twig.

Finally, we test things out by clicking around the app and confirm that the dynamic URLs are working perfectly. All that's left is for you to go and update any remaining links to use the shiny new helper! This is a really good step for maintainability and is best practice moving forward.

Episode discussion

No comments, yet. Be the first!