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
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
190
14 Years of iOS: Lessons and Key Points
seyfoyun
1
770
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
3
1.1k
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
testcontainers のススメ
sgash708
1
120
fs2-io を試してたらバグを見つけて直した話
chencmd
0
220
あれやってみてー駆動から成長を加速させる / areyattemite-driven
nashiusagi
1
200
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
300
快速入門可觀測性
blueswen
0
330
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
A Tale of Four Properties
chriscoyier
157
23k
Scaling GitHub
holman
458
140k
Producing Creativity
orderedlist
PRO
341
39k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
How to train your dragon (web standard)
notwaldorf
88
5.7k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Site-Speed That Sticks
csswizardry
2
190
4 Signs Your Business is Dying
shpigford
181
21k
Into the Great Unknown - MozCon
thekraken
33
1.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
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