Use the left/right arrow keys to navigate.

PHP Microframeworks

Johnson Page — @jwpage

Sinatra

require 'sinatra'
require 'erb'
get '/hello/:name' do |name|
    @name = name
    erb :hello  
end

Sinatra Clones

  • Filters, Templates, Helpers
  • No Rack middleware.
  • Harder to test/mock requests
  • Less DSL-ish syntax.

Hello, World!


  • Slim
  • Limonade
  • Fitzgerald
  • Silex (based on Symfony2)
  • Lithium (as a microframework)

Slim

<?php
require_once 'slim/Slim.php';

Slim::init();

Slim::get('/hello/:name', function($name) {
    Slim::render('hello.php', array('name' => $name));
});

Slim::run();

Limonade

<?php
require_once 'lib/limonade.php';

dispatch('/hello/:name', function() {
    return render(
        'hello.php', 
        array('name' => params('name'))
    );      
});

Fitzgerald

<?php
require_once 'lib/fitzgerald.php';

$app =  new Application();

$app->get('/hello/:name', function($name) {
    return $this->render('hello', array('name' => $name));
});

$app->run();

Silex

  • http://github.com/fabpot/silex/
  • PHP 5.3+
  • Based on Symfony2, proof of concept.
  • Silex: Little documentation, no tests.
  • Symfony2: Well documented and tested.
<?php
require_once 'silex.phar';

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\Engine;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Silex\Framework;

$framework = new Framework(array(
    'GET /hello/:name' => function($name) {
        $loader = new FilesystemLoader('views/%name%.php');
        $view = new Engine($loader);
        return new Response($view->render(
            'hello', 
            array('name' => $name)
        ));
    }
));

$framework->handle()->send();

Lithium (as a microframework)

<?php

use lithium\action\Dispatcher;
use lithium\action\Request;
use lithium\action\Response;
use lithium\core\Libraries;
use lithium\net\http\Router;
use lithium\template\View;

require_once 'libraries/lithium/core/Libraries.php';
Libraries::add('lithium');

Router::connect(
    "/hello/{:name}", 
    array(
        "http:method" => "GET", 
        "name" => null
    ),
    function($request) {
        $view = new View(array(
            'paths' => array('template' => 'views/{:template}.php')
        ));
        $output = $view->render(
            'template',
            array('name' => $request->name),
            array('template' => 'hello')
        );
        return new Response(array('body' => $output));
    }
);

echo Dispatcher::run(new Request());

Finally

  • Many Sinatra clones, just choose one!
  • Solve simple problems.
  • Know when to switch.

That's all.