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
09. Building the Config class

Episodes

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

Transcript

00:00
Having the ability to store and read config is going to be a
00:03
really important part of any app. So first of all, let's get started on where we're actually going to store this config. So over in the root directory, we're going to create our
00:14
directory called config. And inside of here, we're just going to be a series of PHP files that return arrays with any of the config that we need. Let's get started on just a really simple piece of config.
00:27
And we're just going to create this as our global app config. So let's return out an array in here and let's give our app a name. Now, really importantly, a little bit later, we're going to be pulling in config from ENV files or an ENV file.
00:43
And this makes it a lot easier to change config between your local machine and any other environments. But for now, let's go ahead and just add in the name of our app directly into here. And we're going to look at how to set up a config class.
00:57
So over in app, let's create out a new directory and we'll call this config. And inside of here, let's create out a new PHP class called config. And we're going to build all of this entirely from scratch. So before we fill in anything in our config here, let's
01:11
create out a new service provider. So I'm going to go ahead and just copy the contents of our app service provider, and let's create out a new config service provider file in here, paste this in, and of course, change this over.
01:26
So let's switch this over to config service provider. We don't want anything in boot necessarily here. We know that we want to register this config class that we've just created and add this to our services.
01:37
So for the register method, let's go ahead and access our container. So this get container, and we want to add in with the full name of the config class that we've just created, and we want to return an instance of that. So let's go ahead and return new config and we're done.
01:57
Let's go down to our provide section and add in the full namespace to that config class. So we should now be able to grab out our config instance and use this as we need. Let's just add one of the methods that we're going to need in here to get
02:11
config, and then we'll start to fill everything in. So when we grab an item out of config using this config class, we're going to need a key or an item. Let's just call that key.
02:23
And we'll also want to provide a default here and we'll set that to a default of null if this config item can't be found. Okay. So for now, what I'm just going to do so we can test this out is just return
02:36
anything in here and let's see how we access this config out of our container. Okay. So over in our bootstrap and app file, let's go and add this in as a new service provider.
02:47
We're going to do this first before anything. Eventually we're going to have our service providers listed within our config, but we're always going to need our config service provider in here to be able to access that. Okay.
03:01
Again, let's just do some testing here. So let's kill the page and let's go ahead and var dump on our container. Let's go ahead and get out our config class and let's just say get app.name. So that's eventually what we want here.
03:19
We want to grab from the file that it's in, which is this first part here. And then the second part is going to be any of the keys that are listed inside of there. Okay. Let's go ahead and try this out.
03:31
And yeah, sure enough, we get ABC returned, but of course we need to fill this in to actually make it useful. To do this inside of the config class itself in here, we're going to store an array of config values.
03:45
So let's just call this config here. And let's just go ahead and mock up what this would look like. If we were to read it from a file, we're going to do that in the next episode. But let's say that we have an app.php config file.
03:57
Well, that would come under an app key. And then underneath this, we would have the name value. So in our case, let's just write ABC in there, or we'll define out the full name that we defined.
04:09
So eventually we're going to get to the point where when we create this config class, we're going to read all of the files within that config directory and add them to this kind of structure. What we're interested in now though, is getting this in a dot notation.
04:23
So remember the key could be something like app.name. So we want to extract app.name directly from here. Okay. So how do we do this?
04:33
Well, we're going to use a package called php.notation. And this allows us to grab out things from an array within php in a dot notation style. So let's go and pull this in.
04:45
Let's find the installation section and go ahead and pull this into our project and we'll see how we'll use this. Okay. So this gives us a really convenient dot function.
04:56
So what we can actually do here is return using that dot function. Accessing the config. So that will load that in to that package. Then we can say get, and we can get that at that specific key.
05:09
So that's just going to work and pull it out of this array in a dot notation. Now, if this doesn't exist for any reason, what we want to do is return the default in here. Most of the time that's going to be null, but we can define the
05:22
defaults if we need to at any point. Okay. Let's have a look at what this looks like. Remember over in bootstrap and app, we're grabbing app.name.
05:30
Let's go over to our project, give that a refresh. And there we go. So we're successfully grabbing from a multidimensional structured array. A value like this with dot notation, which is really helpful.
05:43
And of course, if we try and access something that doesn't exist, that's going to give us back null. So if we hop back over, we get null. But if we provide the default in here, so let's just say ABC is the default.
05:54
You can see that gives us the default back. So that's working really nicely. But of course, at the moment over in our config file, we are manually adding this config here.
06:04
What we want to do is read these files in from the config here, apply them to this array, and we then will be able to create any more config files we need and read any of the values that we have inside. Let's tackle that in the next episode.
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!