Playing
03. Bootstrapping and structure

Episodes

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

Transcript

00:00
At the moment, we're assuming that we're going to do everything inside of index.php.
00:05
Now, that's not really what we want, because index.php is just going to be an entry point to our application. We really want a dedicated bootstrap file,
00:14
which handles bootstrapping everything our app needs, running our app so we can respond to routes, and all the other stuff in between. So what we're going to do is we're
00:24
going to create out a new directory in the root of our project. And we're going to call this bootstrap. Inside of here, we're going to create another PHP
00:32
file called app.php. And this is just going to be bootstrapping up our app-related stuff. You could create other bootstrap files later
00:41
if you needed to break away other bootstrap operations. So we're pretty much going to take everything that we've done here with our app and our autoloader, and we're going to pull that over into bootstrapping app.
00:53
So really just moving stuff around in here, and we should be good. So inside of public, what we now need to do is just requiring this bootstrap file.
01:02
So let's go ahead and require this in now. And we're going to go back into bootstrap, pull in app.php. And there shouldn't be any difference now in the way that this works.
01:11
We've just moved everything over to a dedicated bootstrap file. Now that we've done that, let's talk about how this application is actually going to work. Now, we're going to keep this app class,
01:23
because this is going to be responsible for resolving some of our dependencies, like our requests and our responses. And it's going to go ahead and run our app. So the first thing that we really want in here
01:34
is a run method, which is going to run our app. So let's just scaffold that out for now. And we'll come back over to our bootstrap file, and we'll say app and run.
01:45
So what are we going to do in this bootstrap file? Well, we are, at the top here, going to do a little bit of setup, like error reporting. We might disable that and pull in something else
01:54
to show any errors that we've got. Inside and in between creating out our app and running it, we are going to register our routes. Now, we're going to start by doing that directly within this file.
02:06
But then we're going to break this away to a dedicated web routes file. And then, of course, later on, you could create out an API routes file if you wanted to as well. So under our run method that we have here,
02:18
let's do exactly the same thing as we did before. And we'll say run app. And we shouldn't see any difference here. But we've now got a much nicer structure.
02:27
And we should have a good idea inside of bootstrap of what we're doing. So up here in the setup is going to be an incredibly important part of what we're building here.
02:36
And that is going to be working with our container. So very shortly, we're going to jump over to the container section, create a container, pass it into our app. And then that will allow us to register routes with our router
02:49
inside of our container. And then when we run this, it will create out a response for us to respond to any requests via our routes. That is the basics of any framework.
03:00
We do setup. We create out some sort of app container that has our request and our ability to register routes or do anything else we need.
03:09
We register their routes within a file. And then we run the app. That's going to go ahead and respond so the user can actually see something on the page.

Episode summary

In this episode, we're reorganizing our application's structure to get ready for some more advanced features and better code practices.

We start by moving away from putting everything inside index.php. Instead, we're setting up a designated bootstrap directory at the root of our project. Here, we create an app.php file strictly for bootstrapping—meaning, it's responsible for setting up everything our app needs before it can start handling requests. This keeps our code clean, modular, and easier to maintain. We simply require this bootstrap file from index.php now.

We also talk about scoping out the app's responsibilities with an App class, where we'll handle things like managing dependencies (think requests and responses) and kicking the app off using a run method. At this stage, we scaffold the method and wire it up so our bootstrap file runs the app as expected.

There's a bit of prep at the start of bootstrapping, like possibly setting up custom error handling. We also start registering routes (initially in the bootstrap file), but we'll soon break routing out to a dedicated file for better organization. Eventually, we'll support separate files for web and API routes as needed.

Finally, we discuss the basics that almost every framework has: some initial setup, an application container for managing things like requests and routes, routes registration, and finally running the app to respond to incoming requests. This episode lays the groundwork for those best practices!

Episode discussion

No comments, yet. Be the first!