Playing
01. Configuration with Slim 3

Episodes

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

Transcript

00:00
Config is essential within a framework and the nature of Slim 3 means it's not very complicated by design and that means we don't have a really nice way to pull in any configuration options. So what we're going to be looking at in this video is how we can create config files like
00:19
this, so one for our database for example and we could have different drivers and one for our app. And obviously you can create more and you can add to these, it really doesn't matter. The point is we want to be able to use these really easily both within our bootstrap file
00:35
when we maybe load other dependencies like PDO or Eloquent and then we can also use them within our routes if we need to. So let's jump over and see how we get all this set up. So I'm starting out with a very basic Slim 3 project layout.
00:51
I've got my index file here which is requiring in my bootstrap file and we're just running the application and in the bootstrap file we're just creating a new app instance and then I've got this routes file that I'm including and at the moment we just have a get route to the route URL and all I'm doing is echoing out home.
01:10
So if we go over to the browser, that's what we see. So the goal here is to within bootstrap on the Slim container create a PDO dependency and then what we can do is access our database. That's not the point but what we want to do is create the configuration for this and we
01:28
can see how we use this really easily. So the first thing I want to do is create a container instance. So this is a new slim container and then what we do is we pass that into app. So now what we can do is we can add any dependencies to the container.
01:48
So we can add things to the container and use them. So the idea here is going to be something like container config and then we're going to have a callback here which is going to return our configuration. Now this configuration and I'm just going to put a C in there if we need it later on.
02:07
Our configuration isn't going to be just loading in a file or just, you know, outputting an array. We're actually going to be using a package for this. It's a really nice clean package and it's over here on github-hassankhan-config.
02:24
So what we're going to be doing is downloading this, we're going to be pulling that in as a dependency and then for when we add databases or any other connections for example if you use mail then we can use this config and it's really nice. So if we scroll down on the github documentation we see that we can just require it in with
02:43
the vendor name and the package name. So I'm just going to copy this over, you can go ahead and type that. I'm currently within the root directory of my slim project so I can just paste this in. Obviously I assume you'd have composer installed and we'll wait for that to go and download.
03:00
There we go. So we now have this package. It's over in here so we're all good to go. So how do we actually use it?
03:09
Well it's as simple as just returning a new instance and then passing in the config files you want to load in. So we're going to return a new noodlehouse-config. This might seem a bit confusing, it's just namespaced under its old owner's name.
03:30
And inside of here we pass an array with files. Now there are plenty of ways that you can pull in configuration using this package. It supports PHP, INI files, XML, JSON and YAML. So you can choose what you want.
03:46
Now for now I'm actually just going to get rid of this because we're going to add PDO to our container first and then we can go and create our config files and replace them later. So typically what we would do is we'd say container DB and we have our closure just
04:04
here, it should be an equals. And inside of here we want to return a new PDO instance. So normally you would say something like MySQL, you have a host, DB name, we'll just say website and then you have a username and a password so we can use PDO.
04:29
So just to test this out under the roots I'm going to create a very quick query. So to access container items we just say this and then the name of the item in the container and then we just can run a query. So I'm going to say select one.
04:44
That doesn't do anything, it will just return one and then we can do something like fetch and I suppose we could do a file dump on that like so. So this will give us the following. We just get a result back with one in.
05:03
Okay, so now that we've got everything set up, we don't want to hard code our config within here. So it's going to get really messy. So within my main project directory I'm going to create a new folder called config and then
05:16
I'm going to separate out into each file depending on what it is I'm creating configuration for. So in our case we'd create a db.php file and all we're going to do in here is return an array and that might seem a bit strange but what happens is when you load a file in that returns an array, you have an array then to work with.
05:41
So in our case what we can do is we can create a key for a particular driver. This means that you can have multiple drivers in here if you wanted to and we can start to define our configuration. So I'm just going to write exactly what I wrote that I hard coded in.
05:57
So the database name is website, the username is root and our password is root as well. Okay so we now have some basic configuration. Now we need to load it into the config package that we've pulled down. So to do this, like I said before, we can use an array of multiple files we want to
06:24
load in. Now in our case we're loading in a PHP file so all we need to do is just pull this in. So we just say config db.php. Now what we're going to do is test out pulling in this config within this container closure
06:43
here and we can access that using C here. So if we do a var dump on C config we can now access our config directly with this C variable and we just kill the page there. So if I refresh now you can see we now have a noodle house config object and we can use
07:05
the functionality on here to pull out configuration options. So to actually pull out configuration we use the get method and from here we just use the keys so we say mysql.host, it's in dot notation but what I'm actually going to do is I'm going to kind of namespace these under each file.
07:29
So what I want to do is say db.mysql.host or db.mysql.password. So that just makes it a little bit clearer. So under here then I'm going to say db.mysql.host all the way to the end to the configuration option I want to pull out.
07:50
So now when I refresh here you can see we get the IP address of the server we're connecting to. So now let's go and replace it inside of our config here and we'll see if the query still works.
08:04
So for this I'm just going to pull these down onto each line, each argument just so it's a little bit tidier and obviously in here I'm just going to append this on. So we'd say c config get db.mysql.host and we can just copy this and paste it for the db name as well.
08:27
So we just append onto the end of that string and that would be db name and obviously we can do the same for the username and we can do the same for the password. Now you might think, oh this does look a little bit messy and you're kind of right it does but the benefit of doing this is that you have separate configuration that you can jump
08:52
straight into without having to look at any of your code. The other benefit here is that you can have different configuration files on different servers depending on your credentials so it makes it a lot easier to just switch these out.
09:06
So now let's just return back to our example on here just to make sure that this still works and it looks like it does, perfect. So now that we've done that, we have used configuration within another container item we're creating, what about accessing configuration from our roots?
09:25
This can be very useful. So I'm going to create a new configuration file and I'm going to call this app.php. This is just going to be general app configuration. So I'm going to return an array here, again we're going to kind of namespace it under
09:40
app and inside of here I'm going to say site name or we could just say name. So here I'm going to say config test. Okay so now that we've got this, we can access this automatically as long as we add in this file so we can just say app.php.
10:04
You could of course come up with a clever way to iterate through these configuration files and load them all in, that's absolutely fine as well, just make sure you're really careful in terms of any security and requiring any files in that may be in here. So now under roots what we can do is access config.
10:22
So because it's on our container, we can do the same thing as we did with our db dependency, we can just echo out this config get so the syntax here is nice and then we can say app.name. So here you'd expect to see config test. So that's how easy it can be when you're using a package like this to go ahead and set up
10:47
configuration. Making the effort to do this initially will save you a lot of time in the future, like I said before we can just jump over to these files, change any configuration and we're good to go.
1 episode 11 mins

Overview

Easily set up powerful, flexible configuration within Slim 3 and make it easier to switch your application settings.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!