Slim PHP 101 - Using Laravel config package
Over the past few years Laravel have become a popular full stack PHP framework and with its latest version (L4) the framework have been componentized. Now we can make use of most of Laravel components outside of the framework, the author of the framework have created packages under the name illuminate.
Today I will show you how to integrate the Config package into Slim and make use of it. Before we move on to that, some may ask what value will this add to Slim since we can already store settings inside of Slim. With the Config package we can store settings into separate files and we can also make use of the cascading folder structure for different environments.
Here is an example of the structure we can have below.
This means that when we request for a config, the Config package will check our environment and load that config in the correct environment directory, if the config is not available in that environment directory, the Config package will go to the root level of the config directory to check.
Note: I will go through setting up an environment variable further on in this article.
Now that we can see one of the benefits (will cover more as the article goes on), we can now move on to the actual code.
Here is a example of what our config.php
file would look like.
We will be using composer to install all our dependencies.
Composer setup
Our composer.json file will look like below.
Config package has its own dependencies which will get installed
Once you have installed all your dependencies we can start setting up our Slim project. We will do all of our setup in our index.php
file for this project. Here is what my index.php
look like.
Lets start adding in the Config package into our current code.
Lets walk through the added code above and explain what each bit is doing (I will intentionally skip the ENVIRONMENT
variable bit as the comment states what it’s doing).
In order to make use of the Config package we need the Filesystem package, we are passing this into the DI container of Slim.
In the code above we now setup the config loader which we pass the Filesystem package into along with our config root path.
Now we are creating our actual config
resource locator and passing in our previously defined loader
resource along with our ENVIRONMENT
constant we defined at the top of our code.
We can now make use of this by calling $app->config
, you can find the API documentation.
What the code above will do is look into our config directory for a file called config.php
then inside of that file look for a key site_name
in the array.
If our environment was production then this would look for a file called config.php
inside of production
directory with the key site_name
in the array as this doesn’t exist in our directory structure we defined earlier in this article it would revert to the root config.php
file.
You will notice that this call is using the dot notation to get information from an array, this is thanks to the Support package helpers.
Here is our complete code below.
I am a freelance web developer who works under the name Silent Works. I work on various open source project and try to evolve with new technologies as they arrive.
Tweet Follow @silentworks