Fat-Free Framework – 3. Creating a configuration file

In the previous tutorial (Fat Free Framework – 2. Moving logic to controllers), we moved the Homepage controller inside the App/Controllers directory. In order to tell the framework we did this, we’ve set a variable called “AUTOLOAD” to point to that particular directory:

$f3->set('AUTOLOAD','App/Controllers');

AUTOLOAD is one of many global variables, that is “the variables used by the framework globally”. F3 is allowing us to change those variables by using the set() method inside our php scripts. Read more about the global variables here: https://fatfreeframework.com/framework-variables#Globals

Of course we can keep the configuration things inside the main index.php and/or wherever else we like, but we can also use a configuration file. What this file does is keep the configuration variables in an easy to read format, so instead of creating a lot of lines that start with $f3->set(‘variable’,’value’); we can have a nice looking file where everything has its own place.

So let us create a setup.cfg file inside our App/Config directory, and move the AUTOLOAD global variable inside it:

[globals]

AUTOLOAD=App/Controllers/

Once we did this we only need to tell the main index file to retrieve the configuration data from that specific file, instead of setting the AUTOLOAD global. So let’s see our main index.php modified:

<?php

require_once('vendor/autoload.php');

$f3 = Base::instance();

$f3->config('App/Config/setup.cfg');

$f3->route('GET /', 'Homepage->index');

$f3->run();

And since we are working with the configuration file, why not also set the debug level to 3, as this will offer us a lot of details when there is an error on our application.

DEBUG=3

Let’s see how our setup.cfg looks like:

[globals]

DEBUG=3

AUTOLOAD=App/Controllers/

And… just to be sure that we did all we can so no one can see our configuration files (although we will also move the App directory outside the public directory, later), let’s create a .htaccess file and put it inside our App:

Options -Indexes

Put an Enter after that line, and save it. Nice. In the next tutorial we will learn more about the configuration files and also move the routes in one such config file.

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.