PHP micro-frameworks
There have been quite a few Sinatra-inspired micro-frameworks popping up for PHP recently. I’m loving this trend. PHP actually almost feels like a real life modern language with 5.3, and micro-frameworks are doing a good job trying to make it pretty.
The first one I saw was Fitzgerald, which looks like this:
include('lib/fitzgerald.php');
class Application extends Fitzgerald {
function get_hello() {
return "Hello, world!";
}
function get_hello_name($name) {
return "Hello, {$name}!"
}
}
$app = new Application();
$app->get('/hello', 'get_hello');
$app->get('/hello/:name', 'get_hello_name');
$app->run();
Fitzgerald was first to the scene but is still my favorite overall – it sucks to have to define a class for your site, but the framework is tiny, easy to use, and keeps to itself.
Limonade showed up next:
require_once 'lib/limonade.php';
dispatch('/hello', 'hello');
function hello() {
return 'Hello world!';
}
dispatch('/hello/:name', 'hello_name');
function hello_name() {
$name = params('name');
return "Hello, {$name}!";
}
run();
Limonade was a nice effort at ridding us of the class definition, but it craps all over the global namespace, and doesn’t support PHP 5.3 lambdas.
And finally came Fat-Free PHP:
require_once 'lib/F3.php';
F3::route('GET /hello', function() {
echo 'Hello, world!';
});
F3::route('GET /hello/@name', function() {
$name = F3::get('PARAMS[name]');
echo "Hello, {$name}!";
});
F3::run();
F3 really feels like Limonade on steroids. I love that they allow lambdas. I hate their parameter syntax though, and I think they try to do too much: F3 also includes a template engine and a database helper, which is a bit overboard for me. They do it all in one file, though, which is commendable.
If someone really embraced PHP 5.3 you could do something much closer to the
gold standard Sinatra, without global namespace pollution. If you had an
http
namespace that accepted lambdas, it could be very sexy:
require 'lib/http.php';
http\get('/hello', function() {
echo "Hello, world!"
});
http\get('/hello/:name', function($name) {
echo "Hello, {$name}!";
});
http\run();
Or, if you just worked within the http
namespace:
namespace http;
get('/hello', function() {
echo "Hello, world!"
});
Hmm… weekend project, perhaps?