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
10. Reading and merging config files

Episodes

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

Transcript

00:00
Okay so over in our app service provider now we're going to add in the ability to merge config into the property that we have within our config. So the first thing that we're going to do is go and set this up as a variable so we can perform any actions on this that we need and then we could either return the result of whatever we're doing here or we could return the config and do
00:22
something in between here. Either way it doesn't really matter we just want to merge this data in. So let's go ahead and instead do something like this return this merge config from files and let's pass the instance of config in so we can call some sort of merge method on our config once we read each of the files and grab the data out. So inside of here let's go and create out this method and we
00:48
could do all of this inside of the config class if we wanted to. I'm going to do it in the service provider but feel free to refactor it over later if you want to. So this is merge config from files and into this we're going to get an instance of our config class so let's pull that in just inside of here. Now before we start to merge anything into our config let's figure out how we're actually
01:12
going to read the data from the files that we've created. Just to make this a little bit more interesting let's create out another config file in here and let's just call this database.php which we will be using a little bit later so I'm just going to create out an example piece of config in here so we know that we can read that. Okay we've got two files now let's look at how we
01:34
read this. The first thing that we'll need is the path to the directory where our config files live so let's grab the current directory that we are in here and then let's go back into that config directory and we'll find it just there. Great so we've got the path to all of our configuration files. Next up we need to grab all of the files within this directory so to do this let's kill
02:00
the page here and let's go and use scan dir to grab all of the files from that path and using this we can just go ahead and start to iterate over it. So let's iterate over all of the files within that path and of course we'll just call that file and why don't we just file dump each of them files out in here. Okay so over to the browser let's give this a refresh and as you can
02:26
see we've got a few items here so we've got app.php and we've got database.php which are the two things that we need but we don't need the dot and two dots which signify the current working directory and backer directory. These will always exist when you do something like scan dir. So to get rid of these we can use array diff and what we can do is pass in what we want to get rid of here
02:52
and what we want to get rid of is a dot, two dots and that should leave us with just our app and database files. We don't want to read these specifically. Okay so how do we get the array from the files that we're working with? Well what we can do is just use require once on these or require it doesn't really matter which one we do but we can require in that path and then we can
03:20
put onto this the file that we want to read which is the file that we are currently iterating through. Now if we var dump on each of these let's see what happens you can see that we get an array with the app config and we get an array with the database config so they are arrays now and we can use these to merge into our config. So over in our config class that we created let's create out a
03:46
merge method which will take this in and merge it. So in here we're going to get some config that we want to merge in and before we do anything let's get rid of this because we just added that as an example. In here we just want to set the config property that we created up there to array merge recursive just in case anything exists within these twice. Merge the config in with the config we're
04:16
passing in and then we'll just return this so we can continue to chain on. So basically take that array that we're reading through each of them files merge them into the config and return this and we should be good. Okay let's go ahead and do that now so let's access the config that we passed into this and let's say merge and we'll take in that to merge. Now crucially what we
04:39
want to merge in is something that looks like this so let's get rid of this temporarily and let's pull this up into an array we want to merge in something like app and then the contents of that. Remember over in each of the config files that we created this doesn't have an overall app key so we need to manually merge that in so we know which one it belongs to. Okay so to do this
05:04
we know that this is going to be the contents of that file this needs to be the name of the file without the php extension. So to do this as long as we are following convention and we have the name of the file.php we can just explode out the file and then grab the first item so this will split that file up so if we had app.php this would break this up into an array of app
05:31
and php and then we want to grab the first one which is the name of the file and the name of the key that we want. Okay so now that we've done this let's get rid of the die just here and let's go ahead and return the config so here we return the newly merged in stuff. Let's go over to the browser give this a refresh and there we go. Okay so let's go back to
05:55
bootstrap and app and you can see now we're accessing app.name. If we try and access this from our database config file so it's database.abc let's change that to database and abc we get def perfect so we're now merging in all of the config files that we are creating and we can now access each of them with a dot notation.
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!