Back to templates

Template details

Fat-Free Framework

A single-file PHP micro-framework for building dynamic web applications with routing, templating, and SQL/NoSQL database support.

About this template

Fat-Free Framework (F3) is an open-source PHP micro-framework designed to build dynamic web applications using a single-file core, a high-performance URL routing engine, and a lightweight template system.

What is Fat-Free Framework?

Fat-Free Framework (F3) is a PHP micro-framework authored by bcosca, distributed as a single base.php file (with optional plug-ins in lib/). It takes PHP route definitions and callback functions as input and produces rendered HTML, JSON, XML, or plain-text responses. The framework runs on any PHP 7.2+ server, including Apache, Nginx, Lighttpd, IIS, and PHP's built-in web server. F3 emphasizes minimalism: no required installation procedure, no complex configuration, and no fixed directory structure.

Key Features

  • Routing engine — Supports dynamic URL tokens (e.g., GET /brew/@count), named routes (GET @beer_list: /beer), ReSTful mapping via $f3->map(), AJAX/sync route modifiers, and beforeRoute/afterRoute event handlers.
  • Template engine — A compiled template engine with directives for inclusion (<include>), conditionals (<check>), repetition (<repeat>), and exclusion (<exclude>); it also works alongside Twig, Smarty, or plain PHP templates.
  • Database support — Off-the-shelf connectivity to MySQL, SQLite, MSSQL/Sybase, PostgreSQL, DB2, and MongoDB, plus lightweight object-relational mappers that need no configuration.
  • Plug-in ecosystem — Optional plug-ins add a unit testing toolkit, Markdown-to-HTML conversion, Atom/RSS feed reading, image processing, Google static maps, a JavaScript/CSS compressor, OpenID consumer, SMTP over SSL/TLS, and a shopping cart.
  • Autoloader — A namespace-aware autoloader that supports multiple search paths (e.g., 'admin/autoload/; user/autoload/') and maps class names to directory nesting.
  • Configuration files — INI-style files can define globals, routes, and ReST maps without writing PHP, letting end-users tweak settings without altering code.
  • Framework variables — A global symbol table separate from PHP globals, with dot-notation access (e.g., $f3->set('hello.world', ...)) and automatic session management via the SESSION variable.

Who is it for?

Fat-Free suits PHP developers who want a micro-framework with no learning curve and no opinionated directory structure. Novice programmers can follow the "Hello World" example and be up and running in minutes, while expert developers can use advanced features like dynamic route handlers (@controller->@action) and ReST interfaces. It is also a good fit for teams building small-to-medium web applications, APIs, and prototypes that need fast iteration without the overhead of a full-stack framework.

What can you do with Fat-Free?

  • API developers: Map HTTP verbs to object methods with $f3->map('cart/@item', 'Item') to create a ReSTful interface that handles GET, POST, PUT, and DELETE requests.
  • Content sites: Build a blog or wiki by defining virtual routes and rendering templates with the <repeat> directive to loop over array data.
  • Multilingual applications: Use the LOCALES variable and dictionary files to serve localized content, supported by the framework's built-in multilingual features.
  • Maintenance workflows: Use $f3->reroute() to redirect users to a "site offline" page, or trigger $f3->error(404) when dynamic route parameters don't match database records.

How does Fat-Free work?

Fat-Free works by front-controller routing: after including base.php (or vendor/autoload.php via Composer), you register one or more routes with $f3->route(), optionally set framework variables with $f3->set(), and then call $f3->run() to start listening for requests. When a request matches a route pattern, F3 executes the associated callback or class method, populates the PARAMS array with URL tokens, and allows the handler to echo output or render a template.

Pros and cons

  • Pros: The entire core is a single file, so deployment is as simple as uploading base.php. No configuration is required for basic use. It supports six SQL/NoSQL databases out of the box and includes many optional plug-ins without bloating the core.
  • Cons: Fat-Free requires PHP 7.2 or later; older versions will produce syntax errors due to new language constructs. ReST PUT and DELETE methods are not implemented by regular HTML forms, so they must be called via AJAX.

Alternatives

  • Slim — Another PHP micro-framework with routing and middleware support.
  • Sinatra — The Ruby micro-framework whose Domain-Specific Language approach inspired Fat-Free's route syntax.

FAQ

What is Fat-Free Framework?

Fat-Free Framework (F3) is a PHP micro-framework that provides URL routing, a template engine, database mappers, and optional plug-ins, all condensed into a single-file core. It is designed to help developers build dynamic web applications quickly without complex configuration or rigid directory structures.

How do I install Fat-Free?

You can start a new project with composer create-project bcosca/fatfree or add it to an existing project with composer require bcosca/fatfree-core. Alternatively, download the distribution package and include lib/base.php directly in your application.

Does Fat-Free support databases?

Yes, Fat-Free supports MySQL, SQLite, MSSQL/Sybase, PostgreSQL, DB2, and MongoDB off-the-shelf. It includes lightweight object-relational mappers (ORMs) for data abstraction and modeling, and no configuration is required to use them.

What PHP version do I need?

Fat-Free requires PHP 7.2 or later. Running it on an older PHP version will cause syntax errors because the framework uses modern language constructs such as closures and anonymous functions.

Does Fat-Free have a template engine?

Yes, F3 includes a compiled template engine that parses directives like <include>, <check>, and <repeat>, and it also supports plain PHP templates. You can additionally use third-party template engines like Twig or Smarty with F3.