Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Silex (confoo)
Search
Igor Wiedler
March 02, 2012
Programming
11
1.7k
Silex (confoo)
Igor Wiedler
March 02, 2012
Tweet
Share
More Decks by Igor Wiedler
See All by Igor Wiedler
Redis Bedtime Stories
igorw
1
240
Wide Event Analytics (LISA19)
igorw
4
890
a day in the life of a request
igorw
0
100
production: an owner's manual
igorw
0
130
The Power of 2
igorw
0
230
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
360
The Moral Character of Software
igorw
1
240
interdisciplinary computing (domcode)
igorw
0
250
miniKanren (clojure berlin)
igorw
1
240
Other Decks in Programming
See All in Programming
Jakarta EE meets AI
ivargrimstad
0
530
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
110
Amazon Qを使ってIaCを触ろう!
maruto
0
400
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
580
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
役立つログに取り組もう
irof
28
9.6k
Quine, Polyglot, 良いコード
qnighy
4
640
subpath importsで始めるモック生活
10tera
0
300
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
Featured
See All Featured
Teambox: Starting and Learning
jrom
133
8.8k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Producing Creativity
orderedlist
PRO
341
39k
Faster Mobile Websites
deanohume
305
30k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
RailsConf 2023
tenderlove
29
900
A better future with KSS
kneath
238
17k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
BBQ
matthewcrist
85
9.3k
Transcript
Wednesday, February 29, 12
• phpBB • Symfony2 • Silex • Composer igorw @igorwesome
Wednesday, February 29, 12
Wednesday, February 29, 12
Silex Symfony2 μ-framework Wednesday, February 29, 12
Silex is not Symfony Wednesday, February 29, 12
Microframework Wednesday, February 29, 12
Wednesday, February 29, 12
What? • Bare bones • Routes mapped to controllers •
The ‘C’ of ‘MVC’ • REST • Single file app Wednesday, February 29, 12
Why? Wednesday, February 29, 12
Sometimes a full-stack framework is too much for a simple
task. Wednesday, February 29, 12
simple Wednesday, February 29, 12
What makes silex special? Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
• concise • extensible • testable Wednesday, February 29, 12
Http Kernel Interface Wednesday, February 29, 12
Response handle(Request $request) Wednesday, February 29, 12
client Wednesday, February 29, 12
request client Wednesday, February 29, 12
reponse client request Wednesday, February 29, 12
clean Wednesday, February 29, 12
PSR-0 Wednesday, February 29, 12
Wednesday, February 29, 12
Silex is not Symfony Wednesday, February 29, 12
Silex is a user interface for Symfony Wednesday, February 29,
12
require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() { return
"Hello world!"; }); Wednesday, February 29, 12
Phar require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() {
return "Hello world!"; }); Wednesday, February 29, 12
Application require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() {
return "Hello world!"; }); Wednesday, February 29, 12
require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app->get('/', function() { return
"Hello world!"; }); Controller Wednesday, February 29, 12
$app->run(); Wednesday, February 29, 12
Wednesday, February 29, 12
<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
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
Wait a minute! Wednesday, February 29, 12
Lambdas λ Wednesday, February 29, 12
$(function () { $("a").on("click", function (event) { alert("Thanks for visiting!");
}); }); Wednesday, February 29, 12
PHP 5.3 Wednesday, February 29, 12
$f = function ($a, $b) { return $a + $b;
}; $f(1, 2); Wednesday, February 29, 12
lazy $f = function () { exit; }; Wednesday, February
29, 12
nested $f = function () { return function () {
return true; }; }; $g = $f(); $value = $g(); Wednesday, February 29, 12
scope $outer = 'world'; $f = function () use ($outer)
{ $inner = 'hello'; return "$inner $outer"; }; => "hello world" Wednesday, February 29, 12
scope $helloWorld = function () { $outer = 'world'; $f
= function () use ($outer) { $inner = 'hello'; return "$inner $outer"; }; return $f(); } Wednesday, February 29, 12
passing $output = function ($info) { echo $info."\n"; }; $doStuff
= function ($output) { $output('doing some magic'); doMagic(); $output('did some magic'); }; Wednesday, February 29, 12
factory $userFactory = function ($name) { return new User($name); };
// ... $user = $userFactory($_POST['name']); Wednesday, February 29, 12
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
Usage Wednesday, February 29, 12
$app->get('/', function () { return "Hello world!"; }); Wednesday, February
29, 12
Dynamic Routing Wednesday, February 29, 12
$app->get('/hello/{name}', function ($name) use ($app) { return "Hello ".$app->escape($name); });
Wednesday, February 29, 12
$app->get('/hello/{name}', function ($name) use ($app) { return "Hello ".$app->escape($name); });
Wednesday, February 29, 12
Controllers Wednesday, February 29, 12
assert $app->get('/blog/{id}', function ($id) { ... }) ->assert('id', '\d+'); Wednesday,
February 29, 12
value $app->get('/{page}', function ($page) { ... }) ->value('page', 'index'); Wednesday,
February 29, 12
bind $app->get('/', function () { ... }) ->bind('homepage'); $app['url_generator']->generate('homepage') Wednesday,
February 29, 12
bind $app->get('/blog/{id}', function ($id) { ... }) ->bind('blog.post'); $app['url_generator'] ->generate('blog.post',
array('id' => $id)) Wednesday, February 29, 12
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
Before & After Wednesday, February 29, 12
$app->before(function () { ... }); $app->get('/', function () { ...
}); $app->after(function () { ... }); Wednesday, February 29, 12
$app->before(function (Request $request) { $loggedIn = $request ->getSession() ->get('logged_in'); if
(!$loggedIn) { return new RedirectResponse('/login'); } }); Wednesday, February 29, 12
$app->after(function (Request $request, Response $response) { // tweak the Response
}); Wednesday, February 29, 12
Methods Wednesday, February 29, 12
• get • post • put • delete • head
• options Wednesday, February 29, 12
$app->get('/posts/{id}', ...); $app->post('/posts', ...); $app->put('/posts/{id}', ...); $app->delete('/post/{id}', ...); Wednesday, February
29, 12
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
Caching Wednesday, February 29, 12
Error Handling Wednesday, February 29, 12
use Symfony\Component\HttpFoundation\Response; $app->error(function (\Exception $e, $code) { return new Response('Whoops!',
$code); }); Wednesday, February 29, 12
$app->abort(404, "Could not find the thing."); Wednesday, February 29, 12
$app['debug'] = true; Wednesday, February 29, 12
Redirecting Wednesday, February 29, 12
$app->get('/', function () use ($app) { return $app->redirect('/hello'); }); Wednesday,
February 29, 12
Pimple Wednesday, February 29, 12
50 NCLOC Wednesday, February 29, 12
$container = new Pimple(); Wednesday, February 29, 12
$app = new Silex\Application(); Wednesday, February 29, 12
Parameters $app['some_parameter'] = 'value'; $app['asset.host'] = 'http://cdn.mysite.com/'; $app['database.dsn'] = 'mysql:dbname=myapp';
Wednesday, February 29, 12
Services $app['some_service'] = function () { return new Service(); };
Wednesday, February 29, 12
$service = $app['some_service']; Wednesday, February 29, 12
Dependencies $app['some_service'] = function ($app) { return new Service( $app['some_other_service'],
$app['some_service.config'] ); }; Wednesday, February 29, 12
Shared $app['some_service'] = $app->share(function () { return new Service(); });
Wednesday, February 29, 12
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
Exposed Services • debug • request • autoloader • routes
• controllers • dispatcher • resolver • kernel Wednesday, February 29, 12
Service Providers Wednesday, February 29, 12
interface ServiceProviderInterface { function register(Application $app); } Wednesday, February 29,
12
Core Service Providers • doctrine • form • http cache
• monolog • session • swiftmailer • symfony bridges • translation • twig • url generator • validator Wednesday, February 29, 12
3rd Party • doctrine orm • pomm (postgres) • predis
• mongo • KyotoTycoon • memcache • rest • markdown • gravatar • buzz • config • solr • profiler • ... Wednesday, February 29, 12
Redis Wednesday, February 29, 12
$app->register(new PredisServiceProvider()); Wednesday, February 29, 12
$app->get('/{id}', function ($id) use ($app) { $key = 'pastes:'.$id; $paste
= $app['predis']->hgetall($key); ... }); Wednesday, February 29, 12
Wednesday, February 29, 12
• smallish sites • well-defined scope • prototyping • restful
apis When to use Wednesday, February 29, 12
and many more... Wednesday, February 29, 12
Wednesday, February 29, 12
New feature: Composer Wednesday, February 29, 12
Wednesday, February 29, 12
{ "require": { "silex/silex": "1.0.*" } } composer.json Wednesday, February
29, 12
$ php composer.phar install Wednesday, February 29, 12
require 'vendor/.composer/autoload.php'; Wednesday, February 29, 12
New feature: Streaming Wednesday, February 29, 12
$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
on github fabpot/Silex fabpot/Pimple Wednesday, February 29, 12
silex.sensiolabs.org Wednesday, February 29, 12
Ω Wednesday, February 29, 12
Questions? joind.in/5971 @igorwesome speakerdeck.com /u/igorw Wednesday, February 29, 12