The PHP Fat-Free framework is the best library I have found in the PHP world for SEO friendly URL routes (eg, /people/{some_username}). It is simple, flexible, and gets the job done with only a 58KB footprint! This can be thought of as the ‘C’ in MVC, for controller, which handles incoming requests and provides the glue between the model and the view. With Fat-Free all the ‘configuration’ is done directly in PHP code. Some might think that a lack of external configuration is a downside. I would agree with that for large scale enterprise type projects. However, for the kind of projects I use with Fatfree (and LAMP stack in general), I absolutely love the simplicity it offers.
To set up Fat-Free routes in Apache, first configure your .htaccess file:
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L,QSA]
This tells Apache to send all requests into the app (which lives at ‘/’) through index.php (unless the file or directory that corresponds to the requested location exists on the file system). This way CSS, images, javascript files, or whatever else we want to serve will be handled directly by Apache, bypassing Fat-Free.
The next step is to setup index.php that includes Fat-Free and defines a route:
require_once "fatfree/base.php"; //Fat-free F3::set('DEBUG', 3); // debug is good for dev, not for live F3::route('GET /example/@name', function() { F3::set("pageTitle", "First try at Fatfree"); echo F3::render('templates/example.php'); }); F3::run(); // kick off Fat-Free routing
This route specifies that GET requests to /example/{some string} will be sent to example.php. Go ahead and make the file templates/example.php, and put some great content into it.
Access the pageTitle variable like so:
echo F3::get("pageTitle");
Access the value of @name with:
echo F3::resolve('{{@PARAMS.name}}');
It is very smart to go with a layout for your site. Fat-Free lets us chain templates. To add a header and footer:
F3::route('GET /example/@name', function() { F3::set("pageTitle", "First try at Fatfree"); echo F3::render('templates/header.php'); echo F3::render('templates/example.php'); echo F3::render('templates/footer.php'); }); // if you don't need pageTitle, you can do this as: F3::route('GET /example/@page','templates/header.php; templates/example.php; templates/footer.php');
The nice thing about Fat-Free is, each file is executed it its own sandbox. To share variables between templates you can use PHP globals (just make sure to use them wisely).
To respond to a POST event on the example page, just create another route:
// here is our route from the last example F3::route('GET /example/@name', function() { F3::set("pageTitle", "First try at Fatfree"); echo F3::render('templates/header.php'); echo F3::render('templates/example.php'); echo F3::render('templates/footer.php'); }); // now we'll do a POST that does some processing magic F3::route('POST /example/@name', function() { F3::set("pageTitle", "First try at Fatfree"); include("actions/example_post_logic.php"); echo F3::render('templates/header.php'); echo F3::render('templates/example.php'); echo F3::render('templates/footer.php'); });
This way the routing configuration is controlling when to pull in the logic that handles the POST operation. This very cleanly breaks out the relative concerns into multiple files, without the overhead of a bloated MVC framework.
This tutorial should be enough to wet your appetite. Click here for the official documentation with more examples. There are several additional things Fat-Free does as well, including: dependency auto loading, templating, form handling, ORM, etc.
Things to be aware of about Fat-Free’s routing framework:
- Given that the routing is setup as a PHP file, eventually a site will get large enough to warrant breaking it up into smaller subsets.
- There is some repetition when defining the templates, especially the way I have been doing it here with GET and POST.
- Error handling takes some work to setup. I will do another post on how I approach that.
- Fat-Free is dual licensed as GPL and commercial friendly for a donation. Just be aware of that, and buy a license if you need it.