Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Silex (confoo)

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Silex (confoo)

Avatar for Igor Wiedler

Igor Wiedler

March 02, 2012
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. What? • Bare bones • Routes mapped to controllers •

    The ‘C’ of ‘MVC’ • REST • Single file app Wednesday, February 29, 12
  2. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /some/path RewriteCond %{REQUEST_FILENAME} !-f RewriteCond

    %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </IfModule> Wednesday, February 29, 12
  3. server { location / { if (-f $request_filename) { break;

    } rewrite ^(.*) /index.php last; } location ~ index\.php$ { fastcgi_pass /var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } Wednesday, February 29, 12
  4. $f = function ($a, $b) { return $a + $b;

    }; $f(1, 2); Wednesday, February 29, 12
  5. nested $f = function () { return function () {

    return true; }; }; $g = $f(); $value = $g(); Wednesday, February 29, 12
  6. scope $outer = 'world'; $f = function () use ($outer)

    { $inner = 'hello'; return "$inner $outer"; }; => "hello world" Wednesday, February 29, 12
  7. scope $helloWorld = function () { $outer = 'world'; $f

    = function () use ($outer) { $inner = 'hello'; return "$inner $outer"; }; return $f(); } Wednesday, February 29, 12
  8. passing $output = function ($info) { echo $info."\n"; }; $doStuff

    = function ($output) { $output('doing some magic'); doMagic(); $output('did some magic'); }; Wednesday, February 29, 12
  9. factory $userFactory = function ($name) { return new User($name); };

    // ... $user = $userFactory($_POST['name']); Wednesday, February 29, 12
  10. event listener $emitter = new EventEmitter(); $emitter->on('user.create', function (User $user)

    { $msg = sprintf("User %s created.", $user->name); log($msg); }); Wednesday, February 29, 12
  11. convert $app->get('/blog/{post}', function (Post $post) { ... }) ->convert('post', function

    ($post) use ($app) { $id = (int) $post; return $app['posts']->find($id); }); Wednesday, February 29, 12
  12. $app->before(function () { ... }); $app->get('/', function () { ...

    }); $app->after(function () { ... }); Wednesday, February 29, 12
  13. $app->before(function (Request $request) { $loggedIn = $request ->getSession() ->get('logged_in'); if

    (!$loggedIn) { return new RedirectResponse('/login'); } }); Wednesday, February 29, 12
  14. • get • post • put • delete • head

    • options Wednesday, February 29, 12
  15. use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $app->post('/message', function (Request $request) { mail(

    '[email protected]', 'New message', $request->get('body') ); return new Response('Email has been sent!', 201); }); Wednesday, February 29, 12
  16. Protected $app['lambda_parameter'] = $app->protect( function ($a, $b) { return $a

    + $b; }); // will not execute the lambda $add = $app['lambda_parameter']; // calling it now echo $add(2, 3); Wednesday, February 29, 12
  17. Exposed Services • debug • request • autoloader • routes

    • controllers • dispatcher • resolver • kernel Wednesday, February 29, 12
  18. Core Service Providers • doctrine • form • http cache

    • monolog • session • swiftmailer • symfony bridges • translation • twig • url generator • validator Wednesday, February 29, 12
  19. 3rd Party • doctrine orm • pomm (postgres) • predis

    • mongo • KyotoTycoon • memcache • rest • markdown • gravatar • buzz • config • solr • profiler • ... Wednesday, February 29, 12
  20. $app->get('/{id}', function ($id) use ($app) { $key = 'pastes:'.$id; $paste

    = $app['predis']->hgetall($key); ... }); Wednesday, February 29, 12
  21. $app->get('/some-video', function () use ($app) { $file = $app['app.video_filename']; $stream

    = function () use ($file) { readfile($file); }; return $app->stream( $stream, 200, array('Content-Type' => 'video/mpeg') ); }); Wednesday, February 29, 12