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.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!