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
250
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
240
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
260
miniKanren (clojure berlin)
igorw
1
240
Other Decks in Programming
See All in Programming
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
770
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
良いユニットテストを書こう
mototakatsu
5
2k
Haze - Real time background blurring
chrisbanes
1
510
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
770
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
2
460
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Bash Introduction
62gerente
608
210k
Documentation Writing (for coders)
carmenintech
66
4.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Writing Fast Ruby
sferik
628
61k
How GitHub (no longer) Works
holman
311
140k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
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