Playing
02. Composer and PSR-4 Autoloading

Episodes

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

Transcript

00:00
We are truly starting from scratch here. So we are going to go ahead and make out a folder.
00:04
We're going to open up, up in our editor, and we're going to talk about composer and PSR for auto loading. Okay. Let's start with the folder.
00:12
I'm going to go ahead and make out a directory called no framework, and I'm going to CD into this, and I'm going to open this up with my editor once that's open, of course, we've got a completely empty project here. There is absolutely nothing in here.
00:27
So we need to start to think about the app structure. So to start with, why don't we go ahead and create out a new directory in here that's going to house all of our core framework stuff. So I'm just going to call that app.
00:39
That's just going to be our directory here, where we store all of our service providers, our middleware, all that good stuff that's going to work now to be able to access this, of course, we're going to need some sort of public directory that we can point to.
00:53
So I'm going to create out a directory in here called public. And inside of here, we can just create out a standard PHP file called index dot PHP that our web server will go ahead and hit. So if we just test this out by echoing out framework, we should be able to head
01:09
over to the browser, regardless of how you are set up on your local machine. And we should see this. So I'm using Laravel herd to host this. So I get a nice no framework dot test domain, but however you are, you are
01:23
getting this to work, it doesn't really matter. You should be able to open this up in your browser. And see that index dot PHP directory. Once you have everything configured.
01:31
Okay. So now that we've got this, we need to think about PSR for auto-loading inside of this file and inside of the bootstrap files that we're going to create. We need to access any of the classes inside of the app directory.
01:45
So let's go ahead and just start to create out a core directory in here. That's going to house our app file. So let's create out a PHP class in here called app and go ahead and create that out. So we've got just a standard class in here, which we should be able to new
02:01
up from inside of here and get working. Now at the moment, that's not going to work. Let's show this as an example. So I'm going to go ahead and say new app.
02:10
And why don't we just go ahead and VAR dump app out inside of here. And if we hop over to the browser and give that a refresh yet, sure enough, we don't have our app class available. So that's where composer comes in.
02:23
We want to auto-load everything inside of this app directory. So wherever we're trying to access it, it can always be found. So to do this, we're going to go and create out a file in the root of our projects called composer.json.
02:37
And once we've done this, we're going to go ahead and make sure we have composer installed, which is our dependency manager for PHP. We're going to need this to put in any of the dependencies that our framework is going to use.
02:48
So this is absolutely crucial. So once you do have composer installed, you should just be able to run composer on the command line. And we see an error here because the contents of our composer.json file
02:58
is invalid at the moment, but at least we know that it is installed. So to make this valid, we need a JSON object in here, and we're going to start to auto-load in everything from that app directory so we can access it once we require composer in.
03:13
So let's go ahead and start to do this. To do this, we use an auto-load object and PSR 4. And inside of here, we want to give the root name for everything that contains app. Now this can be anything you want.
03:26
So it doesn't have to be app. It can be the name of your company. For example, we could have code course or whatever you're building. It makes sense just to include this as app.
03:36
That's a pretty standard thing. So from this, where do we want to load from? Well, we want to load from the app directory. Now, just by doing this, once we dump our composer auto-load file and require it
03:49
into index, we'll have access to any of the classes inside of app. So let's go ahead and do that now. So if you notice, we can run composer and that should work now. We're going to run composer dump auto-load and optionally we can provide
04:04
in the optimize flag as well. It doesn't really matter. Okay. So now that we've run this, you'll notice a couple of things.
04:10
We've got this vendor folder created out in here, and that contains a composer directory with all of our class map namespaces. We don't really need to worry about that, but this auto-load.php file just needs to be included anywhere that we bootstrap our application up.
04:25
We just need to do this once and then we will have access to any of these classes. So let's go ahead and require this in. So from here, we're going to require and go back into vendor. We're going to require in auto-load.php, and that is all we need to do.
04:41
So now we need to think about the namespace of this app that we have created this app class that we've created, because when we are working with our app default namespace or our root namespace, just here, this needs to be under that default namespace and then any sub directories that we have under here.
05:01
So we're going to define the namespace out in here as app core, and we should be done. We don't need two slashes just there. So this app class lives under app, which is where we registered this over in compose.json and core is this directory just here.
05:17
Okay. Now that we've done that, let's go over to the browser and let's take a look at what happens. Okay.
05:23
Yeah. So the reason that this isn't working is because over in here, we are not referencing the namespace for this. If you're very new to PHP, we can do this in a couple of ways.
05:32
You can do this inline, or you can do this with a use statement at the top. The first way that this looks like is app core and app. So we can use that up there. So it knows where this lives.
05:44
If we head over, give that a refresh. There we go. We've now got access to this instantiated class, or you can do this inline directly. When you instantiate something, which we're not going to do throughout the
05:56
course, I always just prefer to import it. And if your editor allows this, you can just go ahead and import this at the top, whenever you reference a class. And we'll be doing that throughout the course as well.
06:07
Okay. So there is our basic app structure. Now, anything that we need to create goes inside of app, and then we can have a bunch of other folders in here that use this out as well.
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!