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.
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!