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
17. Defining routes in a separate file

Episodes

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

Transcript

00:00
So let's look at how we can tidy up the routes so we don't have to register them all within Bootstrap and App. Before we do that, let's look at invoking a function that's returned by a PHP file. This is the key to what we want to do here.
00:13
So let's think about the structure of our application and where we want our routes to live first of all. It'd be a good idea in the root of the project to have a routes directory. And then in here we can register as many files as we want. So we're going to create out a PHP file just called web.php.
00:30
But later you could crowd an API.php file for API routes specifically. So much like we did with our config when we returned an array, we can return a function from a PHP file. So let's return a static function and let's just keep this really plain for now
00:48
and just do something in here. So why don't we just var dump routes and just kill that in there. Okay, so how do we invoke this from Bootstrap and App? Well, we just want to go ahead and require this in like we did before.
01:05
So let's require in our routes file, go back a directory into routes and web.php, and then we can invoke this like any other function. So if we go over to the browser now and give that a refresh, yeah, we've got an undefined function here.
01:20
So let's have a look. And yeah, of course, we just need to wrap this because that's going to return to us a function, which we're then going to invoke. Give that a refresh.
01:30
And there we go. So we've invoked a function from another file. Now, why is this useful? Well, when we invoke something, we can pass arguments in.
01:39
So we could pass through to this our router, much like we're using down here. And then we could also pass in our container and anything else that we might need within our routes. So let's take this now, get rid of it, and put this inside a routes and web. We know that in here, we're going to get our router.
02:02
So let's call that router. And we'll also get our container. So let's put our container into there as well. We'll paste this in here.
02:10
And now we can just say, router get, because that's the exact same thing that was getting passed to us before. So now that we've done this, we now have a route registered within a specific file. Give that a refresh.
02:24
And we get exactly the same result as before. So now inside of this specific web file, we can register any other routes that we want. And they are nicely tucked away in their own file. So I've just registered an about route here.
02:40
If we head over to slash about, sure enough, we get that returned as well. Now, if you did want to include any other route files, all you want to do is duplicate this file over with the exact same structure as what we're doing here. And over in bootstrap and app, just require that in as well.
02:59
For example, if you wanted API routes, you can do that as well. And there we go. We now have our routes nicely tucked away. And our bootstrap app file is looking a lot cleaner.

Episode summary

In this episode, we take a look at how to clean up our application's route definitions by moving them out of our main bootstrap/App file and into their own dedicated files. We start by discussing the ideal structure for our project, suggesting the creation of a routes directory at the root level where we can keep separate files for different groups of routes, like web.php for web routes and (later on) api.php for API routes.

We then go step-by-step through the process of creating a new routes file, returning a function from it, and importing that function into the bootstrap file. You'll learn how to invoke the function returned by the routes file, pass necessary dependencies like the router and the container, and how to move your existing route definitions into this separate file.

To wrap things up, we test our setup by registering a new route and confirming it's working. This pattern lets you keep your route definitions organized and your main App file much cleaner. Plus, it's easy to expand by just duplicating the structure for other types of routes, like API endpoints. By the end of this video, your routing logic will be easier to manage and ready for whatever you want to build next!

Episode discussion

No comments, yet. Be the first!