Well... I'm out of ideas on what to write about. Do you have any ideas? Please, write me in any comment section you find, and I will do my best to tell you about that particular subject

Fat-Free Framework – 3. Creating a configuration file

Created at: October 7, 2016; Last update: October 7, 2016

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:

PHP

1

$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.

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:

index.php

PHP

1

2

3

4

5

6

7

8

9

10

11

<?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.

PHP

1

DEBUG=3

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

App/Config/setup.cfg

PHP

1

2

3

4

5

[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:

App/.htaccess

PHP

1

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.