How To Use the Fat Free PHP Framework

PHPPHP Frameworks


Fat Free Framework is a PHP micro-framework that was started in 2009 by Bong Cosca. Following a minimalistic approach, it tends to avoid adding code and structure that are not strictly necessary, while focusing on what really matters.

Why a micro-framework?

This type of minimalistic design is common amongst the so-called micro-frameworks, of which PHP offers a wide choice. Other popular micro-frameworks are: Slim (PHP), Sinatra (Ruby) and express.js (node.js). These frameworks usually have a few advantages like, for example:

  • Being extremely lightweight (Fat Free only amounts for roughly 55kb)

  • Having a gentle learning curve, allowing developers to focus almost exclusively on what matters by not having to change their coding style.

  • Offering many of the functionalities that mature, full-fledged frameworks would usually have.

It goes without saying that choosing a micro-framework like Fat Free is not always the best choice. For big projects where a team of people is expected, a more opinionated and structured framework such as Yii or Zend would likely be a better choice.

Set up a new project with Fat Free

The first steps: downloading the framework unzipping the file within your project’s root folder.

Fat Free only runs on PHP 5.3 and higher. If you’re not sure of the version that you’re currently using, you can check by typing:

/path/to/php -v

After having established that the environment where you’re developing is the right one, create a file called index.php, which is going to be your project bootstrap file. On the first line, include Fat Free:

// FatFree framework
$f3 = require ("fatfree/lib/base.php"); 

Then, you’ll have to tell your application if you’re in development or in production mode by setting this variable:

// Set to 1 when in development mode, otherwise set to 0
$f3->set('DEBUG', 1);


And of course, you’ll have to set up a database connection. Assuming you’re using MySQL:

// MySql settings
$f3->set('DB', new DB\SQL(

Or, if you prefer using SQLite:

$db=new DB\SQL('sqlite:/var/www/myproject/db/database.sqlite'));


A simple query can be called by typing:

$result = $db->exec('SELECT field FROM table WHERE id = "1"');

Or, if you like it, you can use Fat Free’s built-in ORM. The query above would become something like this:

$table = new DB\SQL\Mapper($db, 'table');
$table->load(array('id=?', '1'));
$result = $table->field;

With the DB\SQL\Mapper function, you’re essentially “mapping” a table that is already in your database. Should you instead need to add a new record in your table, you’ll have to type:

$table = new DB\SQL\Mapper($db, 'table');
$table->field = "Here is a value";

Notice: You won’t be able to alter your table using ORM.

Giving a structure to your project

Since Fat Free is a micro-framework, it doesn’t come with a ready-to-use structure for your project, thus you’ll have to create it by yourself. An example of a structure for your project could be:

- api
-- models
- css
- js
- template
- views
- index.php

But of course you’ll be entirely free to use the structure that you love. That’s the best thing about using a non-opinionated framework.


In order to avoid having to include all your classes into your project, Fat Free allows you to use the autoloading feature, which is a way to include classes only at the time you really need them. So, to invoke all our classes, we only need to type:


In our case, api/models/ will clearly be the location where we save all our Model classes. When you invoke a class (e.g. $myClass = new myClass()), Fat Free will automatically look for a file called in the same way (myClass.php) within the autoloaded location.


The next interesting thing is the way Fat Free manages our application’s routing. This is how we define routing to our home page:

$f3->route('GET /',
    function() {
        echo 'This is my Home Page!';

Notice the GET attribute there. If needed, it can be replaced with POST, or even with GET|POST, should you need both of them.
And then there’s obviously a function that defines what that page should do.
You can of course manage different parameters too, using this syntax:

$f3->route('GET|POST /post/@id',
    function($f3) {
        echo 'Post #'.$f3->get('PARAMS.id');

As you can see, everything preceded by @ will be considered a variable parameter.

Templating and Views

Fat Free gives you the ability to have your template and views. To include your template/view in a route command, just write:

$f3->route('GET /',
    function($f3) {
        // Instantiates a View object
        $view = new View;
        // Header template
        echo $view->render('template/header.php');
        // This is a variable that we want to pass to the view
        // Page view
        echo $view->render('views/index.php');
        // Footer template
        echo $view->render('template/footer.php');

In order to set variables to be passed to a view, you can use the $f3->set('nameVariable', 'value') function, and then invoke that same variable into the view (e.g. views/index.php) by typing `<?php echo $nameVariable; ?>. It’s really simple.

In conclusion, these are probably the most useful features that you’ll need when developing your first application with Fat Free framework. Should you need more of them, you can always refer to the official documentation.

<div class=“author”>Submitted by: <a
href=“http://www.marcotroisi.com/”>Marco Troisi </a></div>

Creative Commons License