PHP Web Framework Fat-Free

A Beginner's Guide to MVC, MariaDB, and Composer With Fat-Free Framework
A Beginner's Guide to MVC, MariaDB, and Composer With Fat-Free Framework

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’s name and email.

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

Fat-Free Dependencies
Command Line Installation Of Fat-Free Web Framework

Fat-Free Database Configuration
Gnome Text Editor Displaying Configuration File

Fat-Free People Route
Gnome Text Editor Displaying Custom People Route

Fat-Free People View
Gnome Text Editor Displaying Custom People View

Fat-Free User Reference
Web Browser Displaying User Reference

Fat-Free People Result
Web Browser Displaying Custom People Route Result

For more detailed step-by-step guidance, check out the accompanying YouTube screencast where I walk you through the entire process

Fat-Free Custom View Records In Web Browser

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!

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP, Mastering Blender Python API, and fiction The Algorithmic Serpent, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📚 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.

Leave a Reply

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