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

Silex (phpday)

Silex (phpday)

Igor Wiedler

May 29, 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 (ish)
  2. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /some/path RewriteCond %{REQUEST_FILENAME} !-f RewriteCond

    %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </IfModule>
  3. server { index index.php location / { try_files $uri $uri/

    /index.php; } location ~ index\.php$ { fastcgi_pass /var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }
  4. nested $f = function () { return function () {

    return true; }; }; $g = $f(); $value = $g();
  5. scope $outer = 'world'; $f = function () use ($outer)

    { $inner = 'hello'; return "$inner $outer"; }; => "hello world"
  6. scope $helloWorld = function () { $outer = 'world'; $f

    = function () use ($outer) { $inner = 'hello'; return "$inner $outer"; }; return $f(); }
  7. passing $output = function ($info) { echo $info."\n"; }; $doStuff

    = function ($output) { $output('doing some magic'); doMagic(); $output('did some magic'); };
  8. factory $userFactory = function ($name) { return new User($name); };

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

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

    ($post) use ($app) { $id = (int) $post; return $app['posts']->find($id); });
  11. 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);
  12. Exposed Services • debug • request • autoloader • routes

    • controllers • dispatcher • resolver • kernel
  13. Core Service Providers • doctrine • form • http cache

    • monolog • session • swiftmailer • symfony bridges • translation • twig • url generator • validator
  14. 3rd Party • doctrine orm • propel • pomm (postgres)

    • predis • mongo • KyotoTycoon • memcache • rest • markdown • gravatar • buzz • config • solr • profiler • ...
  15. $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') ); });
  16. $mustBeLogged = function (Request $request) use ($app) { if (!$app['session']->has('userId'))

    { return $app->redirect('/user/login'); } }; $app->get('/user/my-profile', function () { ... }) ->middleware($mustBeLogged);
  17. $app->get('/users/{id}', function ($id) use ($app) { $user = getUser($id); if

    (!$user) { $message = 'The user was not found.'; $error = array('message' => $message); return $app->json($error, 404); } return $app->json($user); });
  18. Ω