Introduction:
In this blog post, we’ll explore the Fat-Free Framework (F3), a lightweight and powerful PHP framework that supports the MVC (Model-View-Controller) design pattern. If you’re a beginner looking to build a simple web application using a modern PHP framework, Fat-Free is an excellent choice due to its simplicity, ease of use, and flexibility.
We will cover how to install Fat-Free 3.9.0 using Composer, and how to connect to a MariaDB database table to retrieve and display data as HTML. By the end of this guide, you’ll have a fully functioning application that connects to a database and outputs data dynamically on your website.
Step 1: Install Fat-Free Framework Using Composer
To start, make sure you have Composer installed on your computer. Composer is a dependency manager for PHP, and we’ll use it to easily install the Fat-Free Framework.
Run the following command in your terminal to create a new project and install Fat-Free 3.9.0:
composer create-project bcosca/fatfree [your-project-name] "3.9.0"
This command will create a new directory with your project name, and install Fat-Free Framework version 3.9.0 inside that directory.
Step 2: Setup Database Connection
In this example, we’ll connect to a MariaDB database. First, make sure you have MariaDB set up on your local machine, or use a remote database. You’ll need the following credentials:
- Host: localhost
- Username: root (or your MariaDB username)
- Password: root (or your MariaDB password)
- Database Name: test_db
Inside your Fat-Free project, create a file called config.ini to store your database connection settings:
[database]
host = "localhost"
user = "root"
password = "root"
dbname = "test_db"
Next, we’ll configure the Fat-Free Framework to read this configuration and connect to the database. Open the index.php file in your project, and add the following code:
<?php // Include Fat-Free Framework $f3 = require('vendor/bcosca/fatfree/core/base.php'); // Load the configuration file $f3->config('config.ini'); // Create a connection to the database using the config file $f3->set('db', new \DB\SQL( 'mysql:host=' . $f3->get('database.host') . ';port=3306;dbname=' . $f3->get('database.dbname'), $f3->get('database.user'), $f3->get('database.password') )); // Set up a simple route $f3->route('GET /', function ($f3) { // Query the database and get all records from the 'users' table $results = $f3->get('db')->exec('SELECT * FROM users'); // Pass the results to the view $f3->set('users', $results); // Render the view echo Template::instance()->render('index.htm'); }); // Run the Fat-Free Framework $f3->run(); ?>
In the above code:
- We set up a database connection using Fat-Free’s \DB\SQL object.
- We route the GET request for the homepage (
/
) to query the users table. - The results are passed to the view (which will be created in the next step).
Step 3: Create the View (HTML Template)
Now, let’s create the index.htm file inside the views folder in your project directory. This template will display the users fetched from the database:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>User List</title> </head> <body> <h1>User List</h1> <!-- Display the users --> <ul> <repeat group="{{ @users }}" value="{{ @user }}"> <li>{{ @user.name }} - {{ @user.email }}</li> </repeat> </ul> </body> </html>
In this template:
- We use Fat-Free’s Template engine to loop through the
users
array and display each user’sname
andemail
.
Step 4: Test Your Application
Now, you can run the application locally. Open your browser and go to http://localhost:your-port
. You should see the list of users fetched from your MariaDB database table, displayed dynamically in the browser.
Adding Screenshots and Embedded Video






For more detailed step-by-step guidance, check out the accompanying YouTube screencast where I walk you through the entire process
Conclusion
The Fat-Free Framework (F3) is an excellent choice for beginner PHP developers who want to build lightweight and maintainable applications quickly. In this post, we’ve demonstrated how to set up a simple Fat-Free application that connects to a MariaDB database and outputs data using the MVC architecture.
For those interested in diving deeper into PHP programming, be sure to check out my book, Learning PHP, and the course, Learning PHP, where I teach PHP from the ground up.
I also offer one-on-one programming tutorials and can assist with updating or migrating Fat-Free Framework applications. If you need help with your PHP projects, feel free to reach out to me through my contact page.
Thanks for reading, and happy coding!