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
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
110
C++でシェーダを書く
fadis
6
4.1k
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
970
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
距離関数を極める! / SESSIONS 2024
gam0022
0
290
Jakarta EE meets AI
ivargrimstad
0
220
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Embracing the Ebb and Flow
colly
84
4.5k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
Teambox: Starting and Learning
jrom
133
8.8k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
430
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Designing the Hi-DPI Web
ddemaree
280
34k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Gamification - CAS2011
davidbonilla
80
5k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
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