#native_company# #native_desc#
#native_cta#

The PHP Fat-Free Framework: Slim Down Your PHP Development

By W. Jason Gilmore
on April 5, 2011

The Perl community has long laid claim to the motto “There’s more than one way to do it“. As a long time member of the PHP community, I often wonder whether our motto should be, “There’s more than ten ways to do it.” The number of competing PHP projects can be staggering at times, as is evidenced by the ten lightweight frameworks I introduced in last year’s article, Top 10 Lightweight Frameworks for PHP Development. While sorting through such a broad selection of solutions can at times be overwhelming, the advantage is that with some patience you will eventually come across a solution which perfectly suits your particular tastes.

While I argue that you should consider using a framework for practically every new project you tackle, precisely which framework you choose isn’t so clear-cut. For instance, robust solutions such as the Zend Framework and symfony offer every imaginable feature one could want in a framework, but come at the cost of a fairly significant learning curve. If you’re keen to start capitalizing upon the great framework-oriented features you’ve heard so much about but aren’t yet ready to invest the significant time required to master one of the more involved solutions, consider investigating one of the lightweight solutions such as the PHP Fat-Free Framework.

In this article I’ll show you just how easy it is to get started building Web sites using this deceptively tiny (55kB) framework.

Incidentally, Fat-Free takes advantage of several features available only to PHP 5.3.0 and newer, therefore if you haven’t yet taken the time to upgrade your PHP installation, you’ll need to do so in order to follow along with the examples found in this tutorial.

Introducing the PHP Fat-Free Framework

The PHP Fat-Free Framework manages to pack an incredible set of features into an incredibly small code base, offering native support for caching, bandwidth throttling, and a template engine. A suite of plugins further enhance Fat-Free’s capabilities, providing developers with the opportunity to integrate databases including MongoDB, process forms, generate CAPTCHAs, and manage user authentication, among other features.

To follow along with the ensuing examples, download Fat-Free and place the unarchived files within a directory accessible by your Web server. You’ll also need to rename the htaccess file included with the download, placing the required period in front of the file name (.htaccess). You’ll also need to enable Apache’s mod_rewrite module if it isn’t already enabled, as Fat-Free uses mod_rewrite to map the website’s various routes to their respective handlers. I also suggest renaming the sample index.php file to something like index.php-orig. The index.php file plays a special role within Fat-Free websites, as it is where you’ll define the website routes and associated route handlers.

Using Clean URLs with PHP Fat-Free

Fat-Free might be used to implement a website consisting of solely static HTML pages and requiring little more than clean URLs. For instance, the following index.php file defines three routes (homepage, /about, and /contact), rendering a static page in response to each respective route request. Notice the use of PHP 5.3’s powerful anonymous function feature, which can significantly reduce the amount of code you’d have to otherwise write.

<!--p

require_once 'F3/F3.php';

F3::route('GET /about',
  function() {
    echo F3::serve('pages/about.html');
  }
);

F3::route('GET /contact',
  function() {
    echo F3::serve('pages/contact.html');
  }
);

F3::route('GET /', 
  function() {
    echo F3::serve('pages/index.html');
  }
);

F3::run();

Save this file in your project home directory as index.php, create a directory called pages which contains the three respective HTML pages, and you’re done.

Creating Dynamic Pages with PHP Fat-Free

Of course, a framework’s advantages really become apparent when creating dynamic pages. You can identify parts of a route as being dynamic by defining a token within the route definition. For instance, the previous index.php file can be refactored to define a dynamic token parameter called @page which could subsequently be used to retrieve the desired page:

<!--p

require_once 'F3/F3.php';

F3::route('GET /', 
  function() {
    echo F3::serve('pages/index.html');
  }
);

F3::route('GET /@page',
  function() {
    $page = F3::get('PARAMS["page"]');
    echo F3::serve("pages/{$page}.html");
  }
);

F3::run();